开发者

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

开发者 https://www.devze.com 2023-03-09 05:12 出处:网络
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

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?


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

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), 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消