Search code examples
c#.netasp.net-web-apiactionfilterattribute

Apply ActionFilterAttribute to the entire WebAPI?


I've setup a custom ActionFilterAttribute for my WebAPI

Is there a way to apply this to all WebAPI controllers at once, versus adding the [ActionFilter] to every single WebAPI controller?


Solution

  • You can add your action filter attribute to global filters which applies to all API controllers from WebApiConfig class's Register method.

    public static class WebApiConfig
    {
              public static void Register(HttpConfiguration config)
              {
                     // Web API configuration and services
                        config.Filters.Add(new TestFilterAttribute());
              }
    }