Introduction
The user request is routed to the appropriate action method and controller in ASP.NET MVC. If you wish, you can execute some logic before or after an action method executes in some cases. There are filters for this purpose in ASP.NET MVC.
Different types of filters are available in MVC. Below is the list of all the filters including the built-in filters and interfaces that must be implemented to create custom filters.
Types of filters in MVC
Authentication Filter
The authentication filter is used to confirm the validity of a user for the requested page. Before any other filter is executed, it is the first to be executed. The IAuthenticationFilter interface is implemented by two methods.
- OnAuthentication, which will call when user verification is completed.
-
OnAuthenticationChallenge, which will call after authorization or authentication fail, after an action method is called, and before your view is rendered.
public interface IAuthenticationFilter {
void OnAuthentication(AuthenticationContext filterContext);
void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext);
}
Authorization Filter
When authorization is needed, this Filter is called. It is called just after the Authentication filter and before the Action filter. This Filter authorizes the user with their role. An authorization filter is implemented by using the "Authorize" attribute.
IauthorizationFilter implements the IauthorizationFilter interface, which only defines one method, "OnAuthorization," called at authorization time.
public interface IAuthorizationFilter {
void OnAuthorization(AuthorizationContext filterContext);
}
Result Filter
Like Action Filter, it modifies your view before rendering. It can also perform extra custom logic following rendering. Therefore, it is mainly used for rendering results that you want to display on the view.
This interface implements IresultFilter, which has two methods, OnResultExecuted and OnResultExecuting, which are called just after your result is created.
public interface IResultFilter {
void OnResultExecuted(ResultExecutedContext filterContext);
void OnResultExecuting(ResultExecutingContext filterContext);
}
Exception Filter
You can use an exception filter, for example, if you need to log exception messages in response to an exception. The "HandleError" attribute provides an inbuilt exception filter. IexceptionFilter implements the IexceptionFilter interface, with only one method defined as OnException, and is called whenever an exception occurs.
public interface IExceptionFilter {
void OnException(ExceptionContext filterContext);
}
ActionFilter
Action filters can be applied to either a controller section or the entire controller to modify how an action is executed.
A custom class within ASP.NET MVC allows you to write logic executed before or after an action method runs. Action methods and controllers can be modified using declarative or programmatic filters. They use declarative methods by applying a filter attribute to an action method or controller class and implementing the corresponding interfaces through programming.
public interface IActionFilter {
void OnActionExecuted(ActionExecutedContext filterContext);
void OnActionExecuting(ActionExecutingContext filterContext);
}