note that this answer runs before MVC 5 / Web API 2
Short answer: MVC and Web API filters are not cross-compatible, and if you want to register them worldwide, you must use the appropriate configuration classes for each.
Long answer: ASP.NET MVC and the web API are designed to work in a similar way, but they are actually different creatures.
The Web API lives in the System.Web.Http , while the MVC lives in the System.Web.Mvc . They will live happily side by side, but one does not contain the other, and despite the similarities in the programming model, the underlying implementations are different. Just like MVC controllers and Web API controllers inherit different classes of the base controller (MVC is simply called Controller and the Web API is called ApiController ). MVC filters and Web API filters are inherited from different FilterAttribute classes (both have the same name in this case, but are separate classes that live in their respective namespaces).
Web API global filters are registered through the HttpConfiguration object available to you in the Register WebApiConfig.cs method if you use the project template with WebActivator:
public static void Register(HttpConfiguration config) {
or otherwise in global.asax.cs:
GlobalConfiguration.Configuration.Filters.Add(new MyWebApiFilter());
Mvc global filters are registered through the GlobalFilterCollection object, which is available to you using the RegisterGlobalFilters FilterConfig.cs method for projects that use WebActivator:
public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
or in the global.asax.cs file using the GlobalFilters.Filters collection for those who do not have a WebActivator:
GlobalFilters.Filters.Add(new MyMvcFilter());
It is worth noting that in both cases you do not need to inherit the corresponding FilterAttribute type. Web API filters should only implement the System.Web.Http.IFilter interface, while MVC filter registration is checked to ensure that your class inherits one of several filter interfaces defined in the System.Web.Mvc .
joelmdev Apr 15 '14 at 20:59 2014-04-15 20:59
source share