Updated March 15, 2023
Introduction to ASP.NET MVC Filter
ASP.NET MVC Filter is a custom class that executes before or after some action or controller method. It is routed to a suitable controller or action method based on user requests. ASP.NET MVC Filter makes available with appropriate requests given by user where we need to execute logic after or before action method performs. These filters are applied to the controller or action method in a programmatic method.
Overviews of ASP.NET MVC Filters
ASP.NET MVC Filters are mainly used to execute the custom logic after or before the execution of some action method, where we can write any custom logic to execute for particular execution.
As we discussed earlier, the user will make a request, and depending on that, it executes before/ after some action or controller method. Once the client makes the request, it comes to the router’s engine and navigates that request to the controller. The controller chooses the appropriate action method to execute. The Controller action method handles the request and response back to the client. Let’s see the flow of the process below,
For executing some logic before or after the action method is executed, let’s follow the below flow process,
ASP.NET MVC Filters are the attributes that enable some logic to be executed either before or after an action method is called upon.
Types of ASP.NET MVC Filters
The Filters are applied through the programmatic or declarative method. The declarative method means applying the filter properties to the controller class or action method. The programmatic method includes interfaces applying or implementing a required interface. In ASP.NET MVC Filters, there are various types of Filters as follows,
- Authentication Filter
- Authorization Filter
- Action Filter
- Result Filter
- Exception Filter
Let’s see the overview of each filter
1. Authentication Filter
The initial filter is executed before the other filters/ action method is executed. The Authentication Filter checks whether the request is coming from a valid user. It implements the interface IAuthenticationFilter, this interface used to build for custom Authentication Filter. Let’s see the definition of the IAuthenticationFilter interface,
2. Authorization Filter
It will execute once after the execution of the authentication filter. The authorization filter is used to verify whether the user has all rights to access a particular page or resource. It implements the interface IAuthorizationFilter, the examples of built-in Authorization Filter are AuthorizeAttribute and RequestHttpsAttribute. Let’s see the definition of this filter,
3. Action Filter
It is executed before the action method starts or after an action’s execution. It implements the IActionFilter interface and has two methods: OnActionExecuting and OnActionExecuted. We can use custom logic before the action method starts, then we need to implement the OnActionExecuting method, and we need to create the custom logic; after the action method, we need to implement the OnActionExecuted method. Let’s see the definition of an Action Filter as follows,
4. Result Filters
They are executed after or before generating the result for the action method. There are various Action result type they are FileResult, ViewResult, JsonResult, RedirectResult, PartialViewResult, ContentResult, RedirectToRouteResult and EmptyResult. Those result types are derived from the ActionResult abstract class. The Result Filters are called only after the Action Filters. An example of a Result Filter is the in-built OutputCacheAttribute. It implements the IResultFilter interface. Let’s see the definition of the IResultFilter interface.
The IResultFilter interface contains two methods, OnResultExecuting and OnResultExecuted. To execute the custom logic before generating the result, we must implement the OnResultExecuting method. For writing custom logic after generating the result, we need to implement the OnResultExecuted method. So we need to implement the IResultFilter interface for creating Custom Result Filter. There will be some set of order lists to execute the filters like in the before execution of action method authorization filter will execute likewise after execution method the exception will execute.
5. Exception Filters
They are executed when the unhandled exception happens during the execution of an action or the filters. An example of Exception Filters is the in-built HandleErrorAttribute. The interface IExceptionFilter is used to build a custom Exception Filter which offers an OnException method executed when an unhandled exception occurs at the time of execution of action or filters. Let’s see the definition of IExceptioFilter,
Some predefined filters are already built by MVC Framework, which is ready to use; they are,
- Authorize
- ValidataInput
- HandleError
- RequireHttps
- OutputCache
Register Filters
Register filters are applied on three levels; they are
- Global Level Filters
- Controller Level Filters
- Action Method Filters
1. Global Level Filters: We can apply filters at the global level in the initial level Application_Start event of Global.asax.cs file with the help of FilterConfig.RegisterGlobalFilters() method. This level of global filters is applied to the controller and the action methods. We can apply HandleError globally in the MVC Application. Let’s see the sample code for Register global filter,
// the class contains in Global.asax.cs file
public class MvcFilterApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
}
}
// App_Start folder contains the FilterConfig.cs
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
2. Controller Level Filters: This filter is applied to the controller class; this level filter allows for entire action methods. Let’s see the sample code Action Filter on Controller; it is applied the methods of HomeController,
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Contact()
{
return View();
}
}
3. Action Method Filters: For this filter, one or more filters can be applied to a particular action method. It is applied to the Index() action method; let’s see the sample code for Action Methods,
public class HomeController : Controller
{
[HandleError]
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return View();
}
public ActionResult Contact()
{
return View();
}
}
Conclusion
MVC Filter is a custom class used to execute before or after some action or controller method executes. In this article, we have seen various types of filters with their definitions and in ASP.NET MVC, registering filters at Global Level is easy. Hope the article helps you to understand.
Recommended Articles
We hope that this EDUCBA information on “ASP.NET MVC Filter” was beneficial to you. You can view EDUCBA’s recommended articles for more information.