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.
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.
// Install Heine.Mvc.ActionFilters as a Cake Addin
#addin nuget:?package=Heine.Mvc.ActionFilters&version=2.0.0

// Install Heine.Mvc.ActionFilters as a Cake Tool
#tool nuget:?package=Heine.Mvc.ActionFilters&version=2.0.0

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 905 6/13/2019
3.2.1-beta0 428 6/12/2019
3.2.0 603 6/4/2019
3.1.0-beta4 622 6/3/2019
3.0.8 944 11/9/2018
3.0.8-beta2 539 11/8/2018
3.0.8-beta1 553 11/8/2018
3.0.7 736 10/25/2018
3.0.7-beta1 583 10/10/2018
3.0.6-beta1 612 9/14/2018
3.0.5 1,288 5/3/2018
3.0.5-beta1 798 5/3/2018
3.0.4 911 4/25/2018
3.0.2 941 4/19/2018
2.0.0 1,113 2/15/2018
1.2.0 1,046 11/15/2017
1.1.9 960 6/27/2017
1.1.8 908 6/26/2017
1.1.7 1,004 3/12/2017
1.1.6 923 3/6/2017
1.1.5 974 11/15/2016
1.1.4 957 11/14/2016
1.0.9 1,123 10/18/2016
1.0.8 962 10/7/2016
1.0.7 1,145 10/7/2016
1.0.6 1,055 10/7/2016
1.0.5 1,045 10/7/2016