I came across a weird behavior but I am not sure if I am on the right track here.
I have a controller which overrides OnException
method of the Controller
base class.
public class ControllerFiltersController : Controller {
public ActionResult Index() {
throw new NotImplementedException();
}
protected override void OnException(ExceptionContext filterContext) {
Trace.TraceInformation(
"ControllerFiltersController Exception: " + DateTime.Now.ToString("hh:mm:ss.fff")
);
}
}
I also have an custom ExceptionFilter as follows:
public class HandleErrorCustom : IExceptionFilter {
public void OnException(ExceptionContext filterContext) {
Trace.TraceInformation(
"HandleErrorCustom Exception Message: " + DateTime.Now.ToString("hh:mm:ss.fff")
);
}
}
Then, I registered it as a global filter:
public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
filters.Add(new HandleErrorCustom());
}
What I expected is here for the controller instance filter to run before global filter since the order of filters which are provided by ControllerInstanceFilterProvider
is Int32.MinValue
and the scope of them is FilterScope.First
.
As also explained here: ASP.NET MVC 3 Service Location, Part 4: Filters
But the result is different:
iisexpress.exe Information: 0 : HandleErrorCustom Exception Message: 06:56:49.972
iisexpress.exe Information: 0 : ControllerFiltersController Exception: 06:56:49.974
This is an ASP.NET MVC 4 application and I am not aware of any changes which effects the filter ordering behavior of ASP.NET MVC 3. What am I missing here?
Summary. In this tutorial, you were introduced to ASP.NET MVC action filters. You learned about the four different types of filters: authorization filters, action filters, result filters, and exception filters.
Filters in ASP.NET Core allow code to run before or after specific stages in the request processing pipeline. Built-in filters handle tasks such as: Authorization, preventing access to resources a user isn't authorized for. Response caching, short-circuiting the request pipeline to return a cached response.
This is expected behavior.
Filter ordering depends on the direction the information is flowing. If the information is flowing into the action, then the order is as you expect it; if the information is flowing back out of the action, then the order is reversed.
For example, assume you have three filters in this order: F1, F2, F3. Assume that these are action filters (meaning, they're listening to ActionExecuting and ActionExecuted). The order the system will run them is as follows:
F1.ActionExecuting()
F2.ActionExecuting()
F3.ActionExecuting()
Action()
F3.ActionExecuted()
F2.ActionExecuted()
F1.ActionExecuted()
Error handlers are, by definition, filters that run on the return-side of actions, so their order is reversed.
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