Type of Filters in MVC Application and Why They're Important


Let's discuss filters in ASP.NET MVC.

What are the Filters?

Filters in ASP.NET MVC are a way to implement cross-cutting logic, (for example, security and logging). Sometimes, we need to execute logic before or after executing an action. For such a scenario, ASP.NET MVC provides us with Filters.
ASP.Net MVC provides us with some built-in Filters:
  • Output Cache- This action filter caches the output of a controller action for a specified amount of time.
  • Handle Error- This action filter handles errors raised when a controller action executes.
  • Authorize- This action filter enables you to restrict access to a particular user or role.
Asp.net MVC Filters are used to inject extra logic at the different levels of MVC Framework request processing. Filters provide a way for cross-cutting concerns (logging, authorization, and caching).
In this article, I will show you the different categories of filters that the MVC Framework supports, how to control their execution, and how to create and use filters. We can create our own custom filters. In every request, your action method in the controller will have to check if the user was right or authorized to perform the action and view its result.
The ASP.NET MVC Framework supports four different types of filters. Authentication Filters are introduced with ASP.NET MVC 5. Each allows you to introduce logic at different points during the request processing
Diagram of Filter types

1. Authorization filters

The AuthorizeAttribute and RequireHttpsAttribute are examples of Authorization Filters. Authorization Filters are responsible for checking User Access; these implement the IAuthorizationFilterinterface in the framework. These filters used to implement authentication and authorization for controller actions. For example, the Authorize filter is an example of an Authorization filter.

2. Action Filters

The Action Filter is an attribute that you can apply to a controller action or an entire controller. This filter will be called before and after the action starts executing and after the action has executed.
Action filters implement the IActionFilter interface that has two methods OnActionExecuting andOnActionExecuted. OnActionExecuting runs before the Action and gives an opportunity to cancel the Action call. These filters contain logic that is executed before and after a controller action executes, you can use an action filter, for instance, to modify the view data that a controller action returns.

3. Result Filters

The OutputCacheAttribute class is an example of Result Filters. These implement the IResultFilter interface which like the IActionFilter has OnResultExecuting and OnResultExecuted. These filters contain logic that is executed before and after a view result is executed. Like if you want to modify a view result right before the view is rendered to the browser.

4. ExceptionFilters

The HandleErrorAttribute class is an example of ExceptionFilters. These implement the IExceptionFilter interface and they execute if there are any unhandled exceptions thrown during the execution pipeline. These filters can be used as an exception filter to handle errors raised by either your controller actions or controller action results.


In this article, we learned about the Type of filters in MVC Application and why they're important. 
Thanks for reading..........