How to add global ASP.Net Web Api Filters?

Solution 1:

The following code, in my Global.asax, works for me:

public static void RegisterWebApiFilters(System.Web.Http.Filters.HttpFilterCollection filters)
{
  filters.Add(new MyWebApiFilter());
}

protected void Application_Start()
{
  RegisterWebApiFilters(GlobalConfiguration.Configuration.Filters);
}

Solution 2:

note that this answer holds true up to MVC 5/Web API 2

Short answer: MVC and Web API filters are not cross compatible, and if you want to register them globally, you must use the appropriate configuration classes for each.

Long answer: ASP.NET MVC and Web API are purposely designed to work in a similar way, but they are in fact different creatures.

Web API lives under the System.Web.Http namespace, whereas MVC lives under the System.Web.Mvc namespace. The two will happily live side by side, but one does not contain the other and despite the similarities in the programming model, the underlying implementations are different. Just as MVC controllers and Web API controllers inherit different base controller classes (MVC's is simply named Controller and Web API's is named ApiController) MVC filters and Web API filters inherit from different FilterAttribute classes (both share the same name in this case, but are separate classes which live in their respective namespaces).

Web API global filters are registered through the HttpConfiguration object available to you in the Register method WebApiConfig.cs if you're using a project template with WebActivator:

public static void Register(HttpConfiguration config)
{
    //stuff before
    config.Filters.Add(new MyWebApiFilter());
    //stuff after
}

or otherwise in the global.asax.cs:

GlobalConfiguration.Configuration.Filters.Add(new MyWebApiFilter());

Mvc global filters are registered by way of a GlobalFilterCollection object, which is available to you through the RegisterGlobalFilters method of FilterConfig.cs for projects that are using WebActivator:

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        //stuff before
        filters.Add(new MyMvcFilter());
        //stuff after
    }
}

or in the global.asax.cs file by way of GlobalFilters.Filters collection for those without WebActivator:

GlobalFilters.Filters.Add(new MyMvcFilter());

It's worth noting that in both cases you do not need to inherit from the appropriate FilterAttribute type. Web API Filters need only implement the System.Web.Http.IFilter interface, while MVC filter registration checks to ensure that your class inherits one of a handful of filter interfaces defined in the System.Web.Mvc namespace.

Solution 3:

As of MVC 4 RC, the correct class name is HttpFilterCollection:

public static void RegisterWebApiFilters(System.Web.Http.Filters.HttpFilterCollection filters)
{
    filters.Add(new MyWebApiFilter());
}

protected void Application_Start()
{
    RegisterWebApiFilters(GlobalConfiguration.Configuration.Filters);
}

Solution 4:

Instead of using global filters I prefer to do this :

[MyWebApiFilter]
public class CustomizedApiControllerBase : ApiController
{
   ...
}

And after that inherit all of api controllers from CustomizedApiControllerBase This approach is more expressive in comparison with global filters in global.ascx file.