Filters In ASP.NET MVC Core.





Introduction.

Filters in ASP.NET Core MVC allow you to run code before or after specific stages in the request processing pipeline. In ASP.net Core MVC user will be routed to appropriate contoller and action if there is need to execute some logic before executing and after executing
MVC Filters are used.

There are many built-in filters available with ASP.NET Core MVC, and we can create custom filters as well.Filters can avoid duplicating code across actions

The following are the list of filter types, interface which must be implemented to create a custom filter class.


Filter types



Each filter type is executed at a different stage in the filter pipeline. below are list of different filters.

Authorization filters:
The Authorization filter will run first and are used to determine whether the current user is authorized or unauthorized for the current request.
we can also create custom authorization filter.

Resource filters:
the Resource filter is the first to handle a request after authorization.
it can run code before and after the rest of the filters executed. it can be run before model binding
it can be used for implementing cache and also crypto filter we see in another article.

Action filters:
the Action filter can run code immediately before and after an individual action method is called.
this can be execute before and after any contoller action execution. We can also manipulate the model property value binding.

Exception filters:
the Exception filters are used to unhandled exceptions that occur before anything has been written to the response body.
this filter is generally used to handle exceptions at global, class or contoller action level.

Result filters:
the Result filters can run code before and after the execution of individual action results.

this filter is  executed when the action method has executed successfully.


Filter Order:

As mentioned above, Asp.Net MVC Core includes different types of filters and multiple filters can be applied to a single controller class or action method.
So, filters run in the following order.

1. Authorization filters
2. Resource filters
3. Action filters
4. Exception filters
5. Result filters

Register Filter Scope:

We can register filter into three level in ASP .Net core Mvc application.

1. Global Level.
2. Class level.
3. Method Level.

Global Level.

You can register filter global level into Startup.cs file as startup file is global file same as global.asax file in .net application.
Global filters will be applied to all the controller and action methods of an application.

Code Sample:

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(
config =>
{
config.Filters.Add(typeof(MvcExceptionHandler)); - add you filter which you need to register globally
}
);
}

Class level.

You can implement filter at class level or controller level so, filters will be applicable to all methods of that particular class or controller.

Code Sample:

[MvcAuthentication]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}

Method Level.

You can implement filter at method level so, filters will be applicable to that particular method only.

Code Sample:

public class HomeController : Controller
{
[MvcAuthentication]
public ActionResult Index()
{
return View();
}
}

Same way we can use multiple built-in in filters at different level.








2 comments:

Powered by Blogger.