Types of Filters in ASP.NET CORE MVC

The filter is a piece of logic which you can execute before or after the execution of the action method. Otherwise, without filters, you need to repeat the same piece of logic on each and every controller and action where you want to apply it.

Filter in ASP.NET Core can be applied at different levels

  1. Global
  2. On Controller
  3. On Action Method

There are 5 types of filters in ASP.NET MVC Core

1. Authorization filter

We have 2 types of Authorization filters one is Sync and another is Async, for implementing Sync type we need to inherit IAuthorizationFilter and for implementing Sync type we need to inherit IAsyncAuthorizationFilter.

2. Action Filter

We have 2 types of Action filters one is Sync and another is Async, for implementing Sync type we need to inherit IActionFilter and for implementing Sync type we need to inherit IAsyncActionFilter Filter.

3. Exception Filter

We have 2 types of Exception filters one is Sync and another is Async, for implementing Sync type we need to inherit IExceptionFilter and for implementing Sync type we need to inherit IAsyncExceptionFilter Filter.

4. Result filter

We have 2 types of Result filter one is Sync and another is Async, for implementing Sync type we need to inherit IExceptionFilter and for implementing Sync type we need to inherit IAsyncExceptionFilter Filter.

5. Resource Filter

We have 2 types of Resource filters one is Sync and another is Async, for implementing Sync type we need to inherit IResourceFilter and for implementing Sync type we need to inherit IAsyncResourceFilter Filter.

Installing NuGet Packages

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.AspNetCore.Mvc.NewtonsoftJson

Installing ‘Microsoft.AspNetCore.Mvc.NewtonsoftJson‘ for Setting Json formatting.

Installing ‘Microsoft.EntityFrameworkCore.SqlServer‘ for Using Microsoft SQL Server database provider for Entity Framework Core.

By