Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 898
mrb398 Avatar asked Jun 26 '17 15:06

mrb398


People also ask

Is there a way to apply action filter to all WebAPI controllers?

Is there a way to apply this to all WebAPI controllers at once, versus adding the [ActionFilter] to every single WebAPI controller? Take a look here: stackoverflow.com/questions/14982049/… You can add your action filter attribute to global filters which applies to all API controllers from WebApiConfig class's Register method.

How to add action filter attribute to global filters in web API?

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 ()); } }

What is filter in web API?

Web API Filters. Web API includes filters to add extra logic before or after action method executes. Filters can be used to provide cross-cutting features such as logging, exception handling, performance measurement, authentication and authorization.

What is an action filter in MVC?

An action filter is an attribute that you can apply to a controller action -- or an entire controller -- that modifies the way in which the action is executed. The ASP.NET MVC framework includes several action filters: OutputCache – This action filter caches the output of a controller action for a specified amount of time.


1 Answers

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());
          }
}
like image 162
Mukesh Avatar answered Oct 19 '22 13:10

Mukesh