In what order are filters executed in asp.net mvc

Muhammad Adeel Zahid picture Muhammad Adeel Zahid · Jul 3, 2011 · Viewed 39.9k times · Source

In MVC we can decorate action methods with different filters like

[HttpPost]
[Authorize]
public ActionResult mymethod(){}

HttpPost derives from MethodSelectorAttribute (probably indirectly) and the Authorize attribute inherits from ActionFilterAttribute.

My question is: in which order are they executed in the MVC request pipeline? I tried to go search in MVC source code but failed to find the relevant code bits.

Answer

Eranga picture Eranga · Jul 3, 2011

Filters run in the following order:

  1. Authorization filters
  2. Action filters
  3. Response filters
  4. Exception filters

For example, authorization filters run first and exception filters run last. Within each filter type, the Order value specifies the run order. Within each filter type and order, the Scope enumeration value specifies the order for filters. This enumeration defines the following filter scope values (in the order in which they run):

  1. First
  2. Global
  3. Controller
  4. Action
  5. Last

Extracted from MSDN