Are ASP.NET MVC filters (of the same type) executed in the order they are included in code, and is that guaranteed?

Viewed 63

When including MVC filters, I understand that they can be registered in the following locations

  1. Global (FilterConfig.cs)
  2. Controller Level
  3. Action level

I also know that there are 4 types and that they are executed in the following order:

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

However, I'm a little confused on priority here:

  • Are the locations executed in the order shown?
  • Assuming that the location is executed in order, which takes priority: type or location?
  • What happens if multiple filters of the same type are declared in the same location? Are they executed in the order they are defined?

For example if I have 6 filters as below, in what order will they be executed?

// FilterConfig.cs
filters.Add(new AuthorizationFilter());
filters.Add(new ActionFilter());

-

// MyController.cs
[AuthorizationFilter2]
[ActionFilter2]
public class MyController
{ 
    [AuthorizationFilter3]
    [ActionFilter3]
    public ActionResult MyAction()
    {
         return foo;
    }
} 
0 Answers
Related