Using ASP.NET MVC 3, I am using the RegisterGlobalFilters method to register some gobal exception filters and it seems that the filters are being executed in the reverse order. When using the following code, specifying a lower order number for my NullReferenceException
filter, the default HandleErrorAttribute is executed and the default error page is displayed.
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute()
{
ExceptionType = typeof(NullReferenceException),
View = "Errors/NullReference",
Order = 0
});
filters.Add(new HandleErrorAttribute() { Order = 1 });
}
On the other hand, if I switch the order numbers so the NullReferenceException attribute has an Order value of 1 and the default attribute has an Order of 0, the NullReferenceException attribute handles the error.
From MSDN, "The greater the integer value is, the lower the priority of the filter is." How come I'm seeing the opposite occurring here?
The order of execution for exception filters has changed for exception filters that have the same
Order value. In ASP.NET MVC 2 and earlier, exception filters on the controller that have the same
Order value as those on an action method are executed before the exception filters on the action method. This would typically be the case when exception filters are applied without a specified
Order value. In ASP.NET MVC 3, this order has been reversed so that the most specific exception handler executes first. As in earlier versions, if the
Order property is explicitly specified, the filters are run in the specified order.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
I have read that but this doesn't not apply to my situation as I am applying different Order values for both filters. The problem is that a filter with an Order value of 1 is being fired before a filter with an Order value of 0.
When the system runs the filters, they are sometimes run from the front of the list to the back (forwards), and sometimes run from the back of the list to the front (backwards). Additionally, filters may be skipped in some situations.
IActionFilter.OnActionExecuting is run in forward order, and IActionFilter.OnActionExecuted is run in reverse order. If your OnActionExecuting was never called (because an earlier filter terminated the chain), then your OnActionExecuted will not be called
either.
IResultFilter.OnResultExecuting and OnResultExecuted follows the exact same rules as IActionFilter.
IAuthorizationFilter.OnAuthorization always runs in forward order, and IExceptionFilter.OnError always runs in reverse order.
In earlier versions of MVC, IExceptionFilter.OnError ran in forward order; for MVC 3, we have reversed the order of this filter based on community feedback. Exception filters in MVC have a similar feel to exception handlers in .NET, which unwind from
the inside out. While this reversal is technically a breaking change, we're confident that it now behaves with better predictability.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by nickolsen on Jul 14, 2011 02:35 PM
Thank you for the information. I'm glad that i'm not going crazy. This makes sense in the life cycle of filters but when registering an IExceptionFilter globablly it seems a bit strange to have to order then in reverse.
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.
nickolsen
Member
14 Points
28 Posts
Filter Execution Order Backwards
Jul 12, 2011 04:08 PM|LINK
Using ASP.NET MVC 3, I am using the RegisterGlobalFilters method to register some gobal exception filters and it seems that the filters are being executed in the reverse order. When using the following code, specifying a lower order number for my NullReferenceException filter, the default HandleErrorAttribute is executed and the default error page is displayed.
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute() { ExceptionType = typeof(NullReferenceException), View = "Errors/NullReference", Order = 0 }); filters.Add(new HandleErrorAttribute() { Order = 1 }); }On the other hand, if I switch the order numbers so the NullReferenceException attribute has an Order value of 1 and the default attribute has an Order of 0, the NullReferenceException attribute handles the error.
From MSDN, "The greater the integer value is, the lower the priority of the filter is." How come I'm seeing the opposite occurring here?
Thanks
Nick
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: Filter Execution Order Backwards
Jul 13, 2011 08:15 AM|LINK
From MVC 3 Realease Notes,
http://www.asp.net/learn/whitepapers/mvc3-release-notes
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
nickolsen
Member
14 Points
28 Posts
Re: Filter Execution Order Backwards
Jul 13, 2011 06:52 PM|LINK
I have read that but this doesn't not apply to my situation as I am applying different Order values for both filters. The problem is that a filter with an Order value of 1 is being fired before a filter with an Order value of 0.
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: Filter Execution Order Backwards
Jul 14, 2011 03:39 AM|LINK
http://bradwilson.typepad.com/blog/2010/07/service-location-pt4-filters.html
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
nickolsen
Member
14 Points
28 Posts
Re: Filter Execution Order Backwards
Jul 14, 2011 02:36 PM|LINK
Thank you for the information. I'm glad that i'm not going crazy. This makes sense in the life cycle of filters but when registering an IExceptionFilter globablly it seems a bit strange to have to order then in reverse.
Thanks again.
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: Filter Execution Order Backwards
Aug 17, 2011 07:07 PM|LINK
From my MSDN filtering article:
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.