Order property of ActionFilter, from lowest to greatest or vice versa?

限于喜欢 提交于 2019-12-30 01:59:07

问题


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)]
[Filter2(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?


回答1:


This is the answer I was looking for. Order of OnActionExecuted is reversed order of OnActionExecuting...




回答2:


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. "




回答3:


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), and OnAuthorization(AuthorizationContext) filters run in forward order. The OnActionExecuted(ActionExecutedContext), OnResultExecuting(ResultExecutingContext), and OnException(ExceptionContext) filters run in reverse order.

The ordering is actually quite complex, so take a look at the article for more details.



来源:https://stackoverflow.com/questions/6152420/order-property-of-actionfilter-from-lowest-to-greatest-or-vice-versa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!