Action Filters in .NET Core

Filters and Their Benefits ?

Filters gives us the facility to run a piece of code before or after any stage in request processing pipeline. You are already familiar with filters , let me remind you , you would have used authorization (attribute Authorized) it basically applies filter on your actions and checks is it authorized or not so that is a filter.

Filters have many benefits here are few

  1. Reusability Instead of writing code for each action we write it one time and then use it

  2. Extensible They give use facility to do some ABC action before method and some XYZ operation after that method

Types of Filters ?

We commonly use five types of filters in our applications in same order as they run

  1. Authorization Filters :

    Top most in running order to check is user authorized.

  2. Resource Filters :

    It runs after authorization and before other request pipeline.

  3. Action Filters :

    It runs before and after an action method is called

  4. Exception Filters :

    It is used to handle global exceptions , it gets executed when an unhandled exception occurs.

  5. Result Filters :

    It runs when action method has been excecated , and then before and after execution of action results.

Action Filters

Action filters run immediately before and after an action method is called. It can do couple of things e.g. changing the passed arguments and changing the results.

Today we will see two implementations of action filters

  1. Global
  2. Custom Action Based

Following steps are same for both

  • Create your filter by inheriting class from IActionFilter interface
  • It will ask you to implement those two methods OnActionExecuted and OnActionExecuting
  • OnActionExecuted runs after the method and other one before the method gets called.

Global Action Filter

  • After creating that filter you need to add it in services container for controllers.
  • That’s all you need to do it so this one will run whenever any action gets called

Action Filters in .NET Core

Custom Action Based Filter

  • Add your filter in Program.cs , if you have multiple filters add in similar fashion.
  • Add ServiceFilter(typeof(YourFilter)) at your controller level or action level , that’s all.

Action Filters in .NET Core

When I applied Global Action Filter and Newsletter (Custom Filters) on same controller it executed like this :

Action Filters in .NET Core

Some Key Points

  1. If you want asynchronous action filter then you can inherit from IAsyncActionFilter instead of IActionFilter it has an extra method OnActionExecutionAsync which takes action context and delegate in parameters

  2. For more details you can read here

This article was originally published at https://mwaseemzakir.substack.com/ on .

Whenever you're ready, there are 3 ways I can help you:

  1. Subscribe to my youtube channel : For in-depth tutorials, coding tips, and industry insights.
  2. Promote yourself to 9,000+ subscribers : By sponsoring this newsletter
  3. Patreon community : Get access to all of my blogs and articles at one place