Filter In ASP.NET Core

Filter In ASP.NET Core

· Filters allow us to run custom code before or after executing the action method.

· The filters are invoked on certain stages in the request processing pipeline.

· We can create custom filters as well. Filters help us to remove duplicate codes in our application.

There Four Types of Filters in .NET Core

1. Authorization filters

· The Authorization filters are executed first. This filter helps us to determine whether the user is authorized for the current request.

· It can short-circuit a pipeline if a user is unauthorized for the current request.

2. Resource filters

· The Resource filters handle the request after authorization.

· It can run the code before and after the rest of the filter is executed.

· This executes before the model binding happens. It can be used to implement caching.

3. Action filters

· The Action filters run the code immediately before and after the controller action method is called.

· We can also manipulate the arguments passed into an action.

4. Exception filters

· The Exception filters are used to handle exception that occurred before anything written to the response body.

5. Result filters

· The Result filters are used to run code before or after the execution of controller action results.

· They are executed only if the controller action method has been executed successfully.

Below diagram shows how these filters interact in filter pipeline during request and response life cycle.


Filter supports two types of implementation: synchronous and asynchronous

· The Synchronous filters run the code before and after their pipeline stage defines OnStageExecuting and OnStageExecuted.

 

Synchronous Filter Example

 

using Microsoft.AspNetCore.Mvc.Filters;

namespace Filters

{

    public class CustomActionFilter : IActionFilter

    {

        public void OnActionExecuting(ActionExecutingContext context)

        {

            //To do : before the action executes

        }

         public void OnActionExecuted(ActionExecutedContext context)

        {

            //To do : after the action executes

        }

    }

}

· Asynchronous filters are defined with only single method: OnStageExecutionAsync, that takes a FilterTypeExecutingContext and FilterTypeExecutionDelegate as The FilterTypeExecutionDelegate execute the filter’s pipeline stage.

·  For example, ActionFilter ActionExecutionDelegate calls the action method and we can write the code before and after we call action method.

 

 

 

Asynchronous Filter Example

using System.Threading.Tasks;

using Microsoft.AspNetCore.Mvc.Filters;

namespace Filters

{

    public class CustomAsyncActionFilter : IAsyncActionFilter

    {

        public async Task OnActionExecutionAsync(ActionExecutingContext context,

            ActionExecutionDelegate next)

        {

            //To do : before the action executes

            await next();

            //To do : after the action executes

        }

    }

Adding Filter scope and Order of execution

 

· A filter can be added to the pipeline at one of three scopes: by action method, by controller class or globally (which be applied to all the controller and actions).

· We need to register filters in to the MvcOption.Filters collection within ConfigureServices method.

 

        

Previous Next

Start Your Data Journey Today With MSAInfotech

Take the first step towards data-led growth by partnering with MSA Infotech. Whether you seek tailored solutions or expert consultation, we are here to help you harness the power of data for your business. Contact us today and let’s embark on this transformative data adventure together. Get a free consultation today!

We utilize data to transform ourselves, our clients, and the world.

Partnership with leading data platforms and certified talents

FAQ Robot

How Can We Help?

Captcha

MSA Infotech