I've created a Web Api filter (using System.Web.Http.Filters.ActionFilterAttribute
) but I am unable to get it to work inside of ASP.Net MVC 4. I tried adding it to the RegisterGlobalFilters()
method but that didn't work.
So if one is using Web Api hosted in ASP.Net MVC how does one register filters?
To apply the filter to all Web API controllers, add it to GlobalConfiguration. Filters. public static class WebApiConfig { public static void Register(HttpConfiguration config) { config. Filters.
Filters are actually attributes that can be applied on the Web API controller or one or more action methods. Every filter attribute class must implement IFilter interface included in System. Web. Http.
Global Level Filters You can apply filters at a global level in the Application_Start event of the global. asax. cs file by using default FilterConfig. RegisterGlobalFilters() method.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With