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?
Every filter must implement the three methods in the Filter interface: init(), doFilter(), and destroy().
Filters get executed in the following order for an action: Globally Defined Filters -> Controller-specific Filters -> Action-specific Filters.
TypeFilterAttribute is similar to ServiceFilterAttribute, but its type isn't resolved directly from the DI container. It instantiates the type by using Microsoft. Extensions. DependencyInjection. ObjectFactory.
The ASP.NET MVC framework supports four different types of filters: Authorization filters – Implements the IAuthorizationFilter attribute. Action filters – Implements the IActionFilter attribute. Result filters – Implements the IResultFilter attribute.
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. "
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With