Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filters of GlobalFilterCollection run before Filters of ControllerInstanceFilterProvider

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?

like image 794
tugberk Avatar asked Apr 19 '12 16:04

tugberk


People also ask

How many types of action filters are there?

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.

What are the filters in. net Core?

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.


1 Answers

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.

like image 111
Brad Wilson Avatar answered Nov 10 '22 06:11

Brad Wilson