How to add global ASPI Web Api filters?

I created a Web Api filter (using System.Web.Http.Filters.ActionFilterAttribute ), but I cannot get it to work inside ASP.Net MVC 4. I tried to add it to the RegisterGlobalFilters() method, but that did not work.

So, if you are using Web Api hosted in ASP.Net MVC, how does one register the filters?

+89
asp.net-web-api asp.net-mvc-4
Mar 01 '12 at 17:31
source share
4 answers

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); } 
+102
Mar 01 '12 at 17:53
source share

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) { //stuff before config.Filters.Add(new MyWebApiFilter()); //stuff after } 

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) { //stuff before filters.Add(new MyMvcFilter()); //stuff after } } 

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 .

+77
Apr 15 '14 at 20:59
source share

Like 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); } 
+12
Jun 21 2018-12-12T00:
source share

Instead of using global filters, I prefer to do this:

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

And after that, inherit all the API controllers from CustomizedApiControllerBase This approach is more expressive than the global filters in the global.ascx file.

+7
Mar 01 '14 at 11:20
source share