ASP.NET MVC Filters explained in simple way

The Filters in ASP.NET MVC are used to provide some extra functionality to the action methods in controller.For example,in Index() action method, you are performing a logic to display some data.But before displaying that data you want to perform or check some other important logic , this can be achieved with the help of action filters in asp.net MVC.We will see one by one about each type of filters being used in ASP.NET MVC.

What are the different types of Filters in ASP.NET MVC?
Types of Filters in Asp.Net MVC:-

  • Authorization Filters - Implements IAuthorization filter
  • Action Filters - Implements IActionFilter filter
  • Result Filters - Implements IResultFilter filter
  • Exception Filters - Implements IExceptionFilter filter

Note:
  • Authorization filters are always executed before action filters
  • Exception filters are always executed after other filters

The multiple filters can be applied to single controller or action method and filters run the following order.If you want to change the order by filters order property.

  • Authorization Filters
  • Action Filters
  • Result Filters
  • Exception Filters

Let us see all the filters one by one with examples,


Authorization Filters

The Authorization filters run before an action method.The authorization filters are used to implement authorization for controller action methods. Let us see authorization filter demo Step by Step.
Step #1: Open Visual studio and click File --> New -->Project


Select MVC template and click OK



Step #2: Right click controller and click Homecontroller class.
Step #3: Add [Authorize] attribute to either action method or for the whole controller.Here i have applied authorize attribute for the Index() of HomeController class.
Step #4:

public class HomeController : Controller
{
[Authorize]
public ActionResult Index()
{
    return View();
}
}

Step #5: Now run and see the output. To see the Index method, you need to give login credentials because of Authorize attribute being used in Index() of HomeController class. By default home controller and action methods are routed in "AuthorizeFiltersMVC" project.Go and see Route.config file under App_start folder.


Action Filters

It is just an attribute that you are defining on top of the controller action or entire controller.Action filter is executed before and after action method executes.The action filter attributes can be applied to an individual action or else for the whole controller itself. When the action attribute is applied to the controller then it will be applied to all action methods in that controller.
To know more details about Action Filters,Please click here to see it elaborately.


Result Filters

Result filters are executed before or after the execution of viewresult.It implements IResultFilter attribute. For example,You can very well change the view result before it renders to the web browser.Mostly for writing custom action filter you go for ActionFilterAttribute base class.This class implements both IActionFilter and IResultFilter and it inherits from Filter class. Any custom class which are created by inheriting ActionFilterAttribute will act both as Action Filter and Result Filter The ActionFilterAttribute class has following methods that you can override:

  • OnActionExecuting
  • OnActionExecuted
  • OnResultExecuting
  • OnResultExecuted