This article will help you understand what filters are in ASP.NET MVC and why we should use them. Before moving to the actual definition of filters, don’t be confused by the name, it is not going to filter your data from some list of data.
Basically, filters in ASP.NET MVC are pre-processing and post-processing custom business logic which helps us to add or manipulate our data just before an action is being executed and just after an action has executed. Pre-Processing and Post-Processing logic is not written in any action, we just write it inside the filters. We can use filters for logging visitor details, knowing who is accessing our website, or for some logging information after an action has executed or authentication/authorization logic for the user which is not basically part of our action method.
This article will demonstrate what filters are, why we use them, and how to create custom filters in ASP.NET MVC. Filters can be defined as Controller level, Action level, or Global level. To define it at controller or action level, we have to use it as an attribute, just like below.
To define your filters at a controller level, just decorate the filter as an attribute with a Controller.
- [CustomFilter]
- public class HomeController : Controller
To define your filters at an action level, just decorate the filter as an attribute with Action method.
- [CustomFilter]
- public ActionResult Index()
To define your filters at a global level, just add the filter in a GlobalFilterCollection object in FilterConfig.cs file which is located in the App_Start folder of ASP.NET MVC application.
- namespace FilterExample {
- public class FilterConfig {
- public static void RegisterGlobalFilters(GlobalFilterCollection filters) {
- filters.Add(new HandleErrorAttribute());
- filters.Add(new CustomFilter());
- }
- }
- }
And this is already registered on Application_Start method inside Global.asax.cs file.
- public interface IAuthenticationFilter {
- void OnAuthentication(AuthenticationContext filterContext);
- void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext);
- }
There are different types of filters available in ASP.NET MVC based on their uses in the ASP.NET MVC page life cycle and these are as following.
Authentication Filter
As the name says, it is an authentication filter used for authentication of the valid user while processing the requested page. It is the first filter which executes before any other filter is being executed. It implements IAuthenticationFilter interface which has defined two methods: OnAuthentication, which will be called at the time of user verification; and OnAuthenticationChallenge, which will be called after Authentication or Authorization has failed and an action method gets called before your View is being rendered.
- public interface IAuthenticationFilter {
- void OnAuthentication(AuthenticationContext filterContext);
- void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext);
- }
Authorization Filter
This filter is called when authorization is required. Basically, the sequence of calling this filter is just after the Authentication filter and just before an action filter gets called. It is used to authorize the user with his/her role. For example “Authorize” attribute is used to implement Authorization filter. Authorization filter implements IauthorizationFilter interface which has only one method defined as “OnAuthorization” which will call at the time of authorization.
- public interface IAuthorizationFilter {
- void OnAuthorization(AuthorizationContext filterContext);
- }
Action Filter
This filter is really helpful when we have to perform some pre or post-processing logic with action method executed. Yes, action filter gets called before an action method gets executed and after an action method gets executed. Action filter implements IactionFilter interface which has two methods defined as OnActionExecuted and OnActionExecuting. OnActionExecuting method is called just before an action method getting called and OnActionExecuted method is called just after an action method getting called.
- public interface IActionFilter {
- void OnActionExecuted(ActionExecutedContext filterContext);
- void OnActionExecuting(ActionExecutingContext filterContext);
- }
Result Filter
It is similar to Action Filter and used to modify your View before rendering or performing some extra custom logic just after rendering the View. So, it means, it is mainly used for your result which you want to render on the View. It implements IresultFilter interface which has two methods similar to ActionFilter as OnResultExecuted, called just after your result is created and OnResultExecuting which is called just before your result is declared. For example, there is an inbuilt Result filter which gets used using “OutputCache” attribute that is used to cache your page response.
- public interface IResultFilter {
- void OnResultExecuted(ResultExecutedContext filterContext);
- void OnResultExecuting(ResultExecutingContext filterContext);
- }
Exception Filter
This filter is called when some exception has occurred. For example, if we would like to log exception messages when an exception occurrs then we can use exception filter. There is an inbuilt exception filter available and that is “HandleError” attribute. This implements IexceptionFilter interface which has only one method defined as OnException and it gets called when any exception is generated.
- public interface IExceptionFilter {
- void OnException(ExceptionContext filterContext);
- }
Custom Action Filters
There are several inbuilt filters available with their uses. But if we want to add some custom logic as per our requirement then we can create custom action filter. In this demonstration, we will see, how to create custom action filter to track our visitor data and update page view once a page gets a view.
Before moving to Visual Studio to create an application, just create one database and a few tables that we will use further in this demonstration. Following scripts, we can use this for creating database and tables.


Join the conversation! Your thoughts help the community grow.