Various Filters in MVC

Kailash Chandra Behera | Wednesday, April 29, 2020

Introduction

The goal of this blog is to explain filters in MVC. In this blog we will going to discuss what are the filter available in MVC, how that are working and what are the work sequences and how to configure it.

Getting Started

Filters in MVC allows us to inject some extra logic or to implement your own logic before or after an MVC action executed, before and after response to MVC request. For example, you can tack and write logs in MVC before or after an action is executed.

Filters are introduced in MVC 5 and each allows you to introduce logic at different points during request processing. there are four types of filters available in MVC even you can create your own custom filters.

MVC Filters
  1. Authorization Filter
  2. Action Filter
  3. Result Filter
  4. Exception Filter
Filters in MVC can be applied in three levels that are
  1. Global Level
  2. Controller Level
  3. Action Method Level
Global Level

Global filters will be applied to all the controller and action methods of an application. To apply filters in global level, filters needs to be registered in the Application_Start event of Global.asax.cs file by using default FilterConfig.RegisterGlobalFilters() mehtod.

The below code example shows,how to register filters in global level

 namespace mvcfilters  
 {  
   public class MvcApplication : System.Web.HttpApplication  
   {  
     protected void Application_Start()  
     {  
       FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);  
     }  
   }  
 }  
 namespace mvcfilters  
 {  
   public class FilterConfig  
   {  
     public static void RegisterGlobalFilters(GlobalFilterCollection filters)  
     {  
       filters.Add(new HandleErrorAttribute());  
       filters.Add(new CustomeAuthorizeFilter());  
     }  
   }  
 }  

In the above code two filters are registered, the first one is inbuilt Exception Filter and second one is Custom Authorization filter.

Controller Level

The below code example shows how to Filter over controller.

 namespace mvcfilters.Controllers   
  {   
   [HandleError]   
   public class HomeController : Controller   
   {   
    public ActionResult Index()   
    {   
     return View();   
    }   
   }   
  }   
Action Method Level

The below code example shows how to Filter over action method.

 namespace mvcfilters.Controllers   
  {      
   public class HomeController : Controller   
   {  
    [HandleError] 
    public ActionResult Index()   
    {   
     return View();   
    }   
   }   
  }   

MVC support multiple filters feature, means multiple filters can be applied to a single controller class or action method. If multiple filters are applied to a single controller or action method, then the sequence of execution will be like below.

  1. Authorization Filter
  2. Action Filter
  3. Result Filter
  4. Exception Filter

Above two code example shows how to apply filter or filters over controller and action method.

Related Articles

  1. MVC Exception Filter
  2. MVC Action Filter
  3. MVC Authorization Filters

Summary

In this blog we discussed what are the various filters available in MVC, how filters are working and the sequence of execution, hope you enjoyed.

Thanks