How to use action filters in ASP.Net Core MVC

Take advantage of action filters in ASP.Net Core MVC to execute custom code at specific points in the request pipeline

Current Job Listings

Filters in ASP.Net Core MVC allow us to execute code before or after specific stages of the request processing pipeline. The different types of filters correspond to the different stages of the pipeline, from authorization to result execution.

For instance, you can leverage action filters in ASP.Net Core MVC to execute custom code before and after the execution of an action method. This article presents a discussion of the built-in filters in ASP.Net Core MVC, why they are useful, and how we can use action filters in our ASP.Net Core applications.

Filters in ASP.Net Core MVC

ASP.Net Core MVC contains many built-in filters. These include the following:

  • ActionFilters. These are executed before and after execution of an action method of a controller. 
  • AuthorizationFilters. These filters are executed at the beginning of the request pipeline. They are used to validate a user’s credentials to check if the user is authorized. 
  • ResourceFilters. These filters are executed after authorization and before model binding occurs. You can take advantage of ResourceFilters to implement caching. 
  • ResultFilters. These filters are used to execute code before and after an action method’s IActionResult is executed. 
  • ExceptionFilters. These filters are used to handle any exceptions that occur in the pipeline. You can take advantage of ExceptionFilters to execute custom code when an exception has occurred. 

To continue reading this article register now