7

I have 2 global action filters in my MVC 4 application, that I've registered in Filter.config file using RegisterGlobalFilters. I need them to be executed in a particular order.

I know how to specify order for Controller specific filters but how do I specify order and scope for my global filters? is it in the order in which they are registered?

user1355348
  • 159
  • 2
  • 4
  • This is a great resource for filtering in MVC http://msdn.microsoft.com/en-us/library/gg416513(v=vs.98).aspx – asymptoticFault Aug 13 '13 at 20:00
  • This might help you: http://stackoverflow.com/a/6561914/290343 – Ofer Zelig Aug 20 '13 at 06:59
  • @asymptoticFault, Ofer Zelig: Thanks for the links! They both give order of the different type of filters but not order within global filters. Where I am struggling is how to set order property of global filters because i need my global filters to execute in a particular order. :) – user1355348 Aug 23 '13 at 16:36

2 Answers2

12

As no answer has thusfar been given about how to specify the order of global filters in RegisterGlobalFilters, here's my answer:

You can specify the order in the Add method, by passing in a second parameter:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute(), 1);
    filters.Add(new LogFilter(), 2);
}
Arjan
  • 915
  • 9
  • 12
5

In this MSDN article scroll down to the Filter Order section. There are the Order and Scope properties that allow you to control the execution order.

Steven V
  • 16,357
  • 3
  • 63
  • 76
asymptoticFault
  • 4,491
  • 2
  • 19
  • 24