Heine.Mvc.ActionFilters 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Heine.Mvc.ActionFilters --version 2.0.0
                    
NuGet\Install-Package Heine.Mvc.ActionFilters -Version 2.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Heine.Mvc.ActionFilters" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Heine.Mvc.ActionFilters" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Heine.Mvc.ActionFilters" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Heine.Mvc.ActionFilters --version 2.0.0
                    
#r "nuget: Heine.Mvc.ActionFilters, 2.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Heine.Mvc.ActionFilters@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Heine.Mvc.ActionFilters&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Heine.Mvc.ActionFilters&version=2.0.0
                    
Install as a Cake Tool

Web API Action Filters

To keep your Web API lean and focused, you should think about separating concerns and stick to the DRY principle. This NuGet package contains several reusable action filters that can be used in ApiController.

Installation

Run the following command from the Package Manager Console

Install-Package Heine.Mvc.ActionFilters

or search for the package inside the Nuget Package Manager in Visual Studio.

Registering action filters

There are several ways to register a Web API filter. The following code snippets shows three ways that you can register an action filter depending on your needs.

By action
public class ContactsController : ApiController
{
    [HttpGet]
    [ValidateModel]
    public Contact GetContact(Guid id)
    {
      // ...
    }
}

This will apply only to the Web API action that is decorated with the attribute.

By controller
[ValidateModel]
public class ContactsController : ApiController
{
    // ...
}

This will apply to all of the Web API actions inside the controller.

Globally
using Heine.Mvc.ActionFilters;

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
      config.Filters.Add(new ValidateModelAttribute());
    
      // Other configuration code ...
    }
}

This will apply to all Web API controller acitons in the entire project.

Action filter attributes

  • ValidateModel will validate the ModelState and return a 400 Bad Request in case it is not valid.
  • ReportObsoleteUsage will log a warning message every time a method that is decorated with this attribute is used.
  • ProcessHttpStatusException will catch any exception of the type HttpStatusException (included in the package) and turn it in to the appropriate reponse message in the controller. E.g., if a BadRequestException is thrown in the code excecuted by the controller, it will be handled by the filter and converted into an HttpResponseMessage with status code 400 and message equal to the exception message. Similarily, NotFoundException will result in a 404 response.
  • LogException will log an error message and return 500 Internal Server Error in if an unhandled exception reaches the controller.

Services

If you want more detailed exception logs, you can register the WebApiExceptionLogger service:

using Heine.Mvc.ActionFilters;

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
      config.Services.Add(typeof(IExceptionLogger), new WebApiExceptionLogger());
    
      // Other configuration code ...
    }
}

The difference between the LogException filter and the WebApiExceptionLogger service is that the service will also log a prettyfied version of the incoming request body, which can be useful for debugging purposes. To prevent bloating the logs, you should consider limiting the amount of characters that the service is allowed to log from the request body. To do that, simply set the RequestBodyMaxLogLength property:

new WebApiExceptionLogger
{
  RequestBodyMaxLogLength = 1000
}

The default is 10 000.

Dependencies

The package depends on NLog to handle the logging. That means that you need to have a file named NLog.config inside your project (or a project you are referencing) that configures target, paramaters, minimum log level etc. It also depends on Newtonsoft.Json to prettify the request body.

Product Compatible and additional computed target framework versions.
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.2.1 1,115 6/13/2019
3.2.1-beta0 572 6/12/2019
3.2.0 747 6/4/2019
3.1.0-beta4 955 6/3/2019
3.0.8 1,112 11/9/2018
3.0.8-beta2 694 11/8/2018
3.0.8-beta1 700 11/8/2018
3.0.7 907 10/25/2018
3.0.7-beta1 749 10/10/2018
3.0.6-beta1 778 9/14/2018
3.0.5 1,689 5/3/2018
3.0.5-beta1 1,182 5/3/2018
3.0.4 1,308 4/25/2018
3.0.2 1,437 4/19/2018
2.0.0 1,507 2/15/2018
1.2.0 1,451 11/15/2017
1.1.9 1,246 6/27/2017
1.1.8 1,188 6/26/2017
1.1.7 1,290 3/12/2017
1.1.6 1,201 3/6/2017
1.1.5 1,256 11/15/2016
1.1.4 1,243 11/14/2016
1.0.9 1,537 10/18/2016
1.0.8 1,247 10/7/2016
1.0.7 1,565 10/7/2016
1.0.6 1,464 10/7/2016
1.0.5 1,457 10/7/2016