I defined two ActionFilters:
[DefaultResources(Order = 2)]
[RenderTemplate(Order = 1)]
And to my surprise DefaultResources is executed BEFORE RenderTemplate. But according to MSDN documentation it should work vice versa:
[Filter1(Order = 2)]
[Filte开发者_如何学Pythonr2(Order = 3)]
[Filter3(Order = 1)]
public void Index()
{
View("Index");
}
In this example, action filters would execute in the following order: Filter3, Filter1, and then Filter2.
I'm using .NET 4. And comparing by method OnActionExecuted. Am I missing something?
This is the answer I was looking for. Order of OnActionExecuted is reversed order of OnActionExecuting...
It all depends on what each filter implements.
If DefaultResource implements OnActionExecuting or OnActionExecuted then it will fire first if RenderTemplate does not.
For more details see:
http://www.gregshackles.com/2010/09/custom-ordering-of-action-filters-in-asp-net-mvc/
and
http://msdn.microsoft.com/en-us/library/dd381609.aspx
"The ASP.NET MVC framework will call the OnActionExecuting method of your action filter before it calls any action method that is marked with your action filter attribute. Similarly, the framework will call the OnActionExecuted method after the action method has finished. "
See Filtering in ASP.NET MVC for a full explanation of what determines the order of execution of action filters and their methods.
Regarding OnResultExecuted
, which you said your filters are using, see the following:
The
OnActionExecuting(ActionExecutingContext)
,OnResultExecuting(ResultExecutingContext)
, andOnAuthorization(AuthorizationContext)
filters run in forward order. TheOnActionExecuted(ActionExecutedContext)
,OnResultExecuting(ResultExecutingContext)
, andOnException(ExceptionContext)
filters run in reverse order.
The ordering is actually quite complex, so take a look at the article for more details.
精彩评论