In this tutorial, we will learn about ASP.NET MVC Filters. In ASP.NET MVC Filters are used in controllers. Basically, controllers define action methods that interact user’s request. For example, when the user requests a query, then it is routed to the designated controller, and then the specific or corresponding action method is called. But Sometimes whenever you need to perform logic either before an action method is called or after an action method is run. Then ASP.NET MVC provides filters.
Filter plays a vital role in ASP.NET MVC application. It is one of the main advantages of ASP.NET MVC. You can create extra logic before or after execute controls and action methods. There are many default Action filters in ASP.NET MVC.
Types of Filters
There are four different types of ASP.NET MVC filters.
- Authorization Filters
- Action Filters
- Result Filters
- Exception Filters
Note: – Each filter has its own interface. The interface is the base of action filter. Filters are executed in the order list above. For example, Authorization filters are always executed before the Action Filters and Result Filter and Exception Filters. And similarly, Exception Filters are always executed after the every other type of filter.
It implements the IAuthorizationFilter attribute. These filters are used to authenticate or validate the request whether to execute an action method or not.
E.g. AuthorizeAttribute and RequireHttpsAttribute are authorization filter.
It implements the IActionFilter Attribute. There are two types of methods in action filters: OnActionExecuted() and OnActionExecuting().
It implements the IResutlFilter Attribute. There are two types of methods of Result Filter: OnResultExecuted() and OnResultExecuting().
It implements the IExceptionFilter attribute. There is one method of Exception filter: OnException ().
ASP.NET MVC Filters
Let’s take a look at a simple example by creating a new project.
Step#1: Open Visual Studio, Click on file, New and then Project (File => New => Project).
Step#2: After showing new project dialog, select templates from the left pane, choose the web under Visual C# (Templates => Visual C# => Web). Then choose “ASP.NET Web Application” from the middle pane. Then enter the project name (e.g “FilterDemo”) in the name field, and then set the location of your project in the location Field, and then finally click “OK” button. It will show a new dialog.
Step#3: Choose the MVC project, and then check the MVC check box, and then finally click “OK” button. It will create simple ASP.NET MVC application.
Step#4: To Add a controller, right-click on the controller folder in the solution explorer and then choose Add, and then Controller (Add => Controller). It will show the Add scaffold dialog.
Step#5: Now select the ‘MVC 5 Controller – Empty’ option. And then click “Add” button. Then Add Controller dialog will appear.
Step#6: Enter the name (E.g “Student.cs”) of controller in the Controller Name field and then click “Add” button.
Finally, you will see a new “Student.cs” file in the controller folder.
Apply Action Filter
An action filter can be applied to either an entire controller or an individual controller action.
Let’s modify the index action method in “ StudentController.cs ” with the following action method which will display the current time.
Now Run this URL (http://localhost:62594/student/CurrentTime ) and you will get the following output.
Now if you refresh the browser, then you will see the same time because the action is cached for 10 seconds. It will be updated when you refresh it after 10 seconds.
Apply Authorization Filter
An authorization filter can also be applied to either an entire controller or an individual controller action method.
Let’s add another controller action method in Home Controller.
Now run the following URL http://localhost:54860/Student/authorization, and you will be redirected to the login view page just because of unauthorized person can’t access this specific URL.