Execution Order of Filters in MVC 4 With Practices: Important FAQ

This article describes the execution order of filters in MVC with practices. Custom filters and attributes are an excellent way to inject extra processing logic into the MVC request response pipeline. This is another most frequently asked question in an interview.

interview

In a MVC application the user first lands on a routing module and matches the URL that best fits a route collection, this module parses the request and invokes the respected controller and action and then the controller will render the view in the browser.

Now somewhere we would like to inject some pre-processing or post-processing logic for actions and controllers. In that case we use filters.

You can also have a look at MVC related articles here:

Types of filters

There are the following types of filters that can be implemented to inject custom processing logic.

  • Authorization filter
  • Action filter
  • Result filter
  • Exception filter

 

 Type of filters
 
Let's proceed step-by-step to understand the filters execution order.

filters execution

Step1

Authorization filter: This filter provides authentication and authorization logic. It will be executed before the action is executed. To implement this action an interface IAuthorizationFilter should be implemented by the custom attribute class. Kindly see the image shown below:

image

Step 2: Action filter

This filter will be called before and after the action starts executing and after the action has executed.
  • OnActionExecuting occurs just before the action method is called
  • OnActionExecuted occurs after the action method is called, but before the result is executed (before the view is rendered)
Now to implement this filter we need to create a custom filter attribute class and implement an IActionFilterfilter interface. This interface provides us two methods OnActionExecuting and OnActionExecuted.kindly look at an image depicted below:

code

Step 3: Result filter

This filter executes before and after the result of the action method has been executed. We can use this filter if we want some modification to be done in the action's result.
  • OnResultExecuting occurs just before the result is executed (before the view is rendered)
  • OnResultExecuted occurs after the result is executed (after the view is rendered)
To implement the result filters we need to create a custom filter attribute class and implement the IResultFilter interface. This interface provides two methods OnResultExecuting and OnResultExecuted.
OnResultExecuted
Step 4: Exception Filter

This filter will be invoked whenever a controller or action of the controller throws an exception. This is particularly useful when we need a custom error logging module.

To implement this filter we need to create a custom filter attribute class that implements IExceptionFilter. Here we have also set the ExceptionHandled property to let the filter know that the exception has been handled at the filter level. Kindly have a look at the image depicted below:
Exception Filter

Now press F5 and see the output to know the execution order of filters. Please see the depicted image below:
F5
result

I hope you enjoyed this and that it may help you down the line.

Up Next
    Ebook Download
    View all
    Learn
    View all