
Customizing ASP.NET Core 6.0
By :

Action filters are a little bit like middleware because they can manipulate the input and the output but are executed immediately on a specific action or on all actions of a specific controller on the MVC layer, and MiddleWare works directly on the request object on the hosting layer. An ActionFilter
class is created to execute code right before or after an action is executed. They are introduced to execute aspects that are not part of the actual action logic: authorization is one example of these aspects. AuthorizeAttribute
is used to allow users or groups to access specific Actions or Controllers. AuthorizeAttribute
is an ActionFilter
. It checks whether the logged-on user is authorized or not. If not, it redirects to the login page.
Note
If you apply an ActionFilter
globally, it executes on all actions in your application.
The next code sample shows the skeletons of a normal action filter and an async ActionFilter
:
using Microsoft.AspNetCore...