Filters allow you to run code at certain stages of the request processing pipeline. In other words, we may run code before or after certain phases of the request processing pipeline by using filters in ASP.NET Core MVC 5.

ASP.NET Core MVC contains many built-in filters. Since each filter has a distinct purpose, they are executed at different stages in the filter pipeline. The different types of filters correspond to the various pipeline stages, from authorization to execution of results. In addition to built-in filters for authorization, exception handling, etc., you can also write your own custom filters.

Create an ASP.NET Core MVC project in Visual Studio 2019

First off, you can create an ASP.NET Core project in Visual Studio 2019. Following these steps to create a new ASP.NET Core MVC 5 project in Visual Studio 2019.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.NET Core Web App (Model-View-Controller)” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window, specify the name and location for the new project.
  6. Optionally check the “Place solution and project in the same directory” check box, depending on your preferences.
  7. Click Next.
  8. In the “Additional Information” window shown next, select .NET 5.0 as the target framework from the drop-down list at the top. Leave the “Authentication Type” as “None” (default).
  9. Ensure that the check boxes “Enable Docker,” “Configure for HTTPS,” and “Enable Razor runtime compilation” are unchecked.
  10. Click Create.

Action filters in ASP.NET Core MVC

Action filters are invoked both before and after the execution of an action method. You can use custom action filters to execute reusable code before or after an action method is executed.

You can build a custom action filter by extending the IActionFilter interface and implementing its members. With the help of the sample code provided below, you can create a simple custom action filter in ASP.NET Core MVC 5.

Authorization filters in ASP.NET Core MVC

Authorization filters are executed at the beginning of the request pipeline, before any other filters have been executed. They are used to verify a user’s credentials in order to identify whether or not the user is allowed to access the resource.

The following code snippet illustrates how you can implement a custom authorization filter.

Resource filters in ASP.NET Core MVC

Resource filters are executed immediately after authorization, i.e., directly after the authorization filter has been executed. You can take advantage of a resource filter to implement caching or to short-circuit the filter pipeline.

The following code snippet illustrates how you can implement a resource filter by extending the IResourceFilter interface and then implementing the OnResourceExecuting and OnResourceExecuted methods.

Result filters in ASP.NET Core MVC

In ASP.NET Core MVC, result filters are executed before or after action filters are executed in the filter pipeline. You can create your own result filter class by extending the IResultFilter or IAsyncResultFilter interface.

The IResultFilter interface comprises two method declarations, i.e., OnResultExecuting and OnResultExecuted.

While the former is called before the result from an action method has been processed, the latter is called immediately after the result from an action method has been processed.

Exception filters in ASP.NET Core MVC

Exception filters are used to deal with any exceptions that may arise throughout the pipeline. You can leverage exception filters to apply global policies to unhandled exceptions in your application and to execute custom code when an exception has occurred.

To build an exception filter, create a class that inherits from the IExceptionFilter interface and implements each of its members. If you want to create your own custom exception filter, you can do so by extending the abstract class named ExceptionFilterAttribute and overriding the OnException method of that class. Remember that the abstract class ExceptionFilterAttribute implements the IExceptionFilter interface.

The following code sample demonstrates how to build a custom exception filter class by extending the IExceptionFilter interface and implementing its OnException method.

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
Editor @ DevStyleR