How to Use Filters and Attribute Class In ASP.NET MVC

ASP.NET MVC provides a simple way to inject your piece of code or logic either before or after an action is executed. This can be achieved by using filters and attribute classes.

Types of Filters

The ASP.NET MVC framework provides five types of filters and executes in the same order as given below,

  • Authentication filters
  • Authorization filters
  • Action filters
  • Result filters
  • Exception filters
Create an action method in HomeController and declare Attribute classes Above Action Method. 

  1. public class HomeController: Controller  
  2.   
  3. {  
  4.     [CustomAuthorizationAttribute]  
  5.     [CustomActionAttribute]  
  6.     [CustomResultAttribute]  
  7.     [CustomExceptionAttribute]  
  8.   
  9.     public ActionResult Index()   
  10.     {  
  11.   
  12.         ViewBag.Message = "Index Action of Home controller is being called.";  
  13.         return View();  
  14.     }  
  15. }  

Now, Create a Filters Folder in your application and add the following attribute classes.

  1. CustomAuthorizationAttribute.cs
    1. public class CustomAuthorizationAttribute: FilterAttribute, IAuthorizationFilter  
    2.   
    3. {  
    4.     void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)   
    5.     {  
    6.         filterContext.Controller.ViewBag.OnAuthorization = "IAuthorizationFilter.OnAuthorization filter called";  
    7.     }  
    8. }  
  2. CustomActionAttribute.cs
    1. public class CustomActionAttribute: FilterAttribute, IActionFilter   
    2. {  
    3.     void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)   
    4.     {  
    5.         filterContext.Controller.ViewBag.OnActionExecuting = "IActionFilter.OnActionExecuting filter called";  
    6.     }  
    7.     void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)   
    8.     {  
    9.         filterContext.Controller.ViewBag.OnActionExecuted = "IActionFilter.OnActionExecuted filter called";  
    10.     }  
  3. CustomResultAttribute.cs
    1. public class CustomResultAttribute: FilterAttribute, IResultFilter  
    2. {  
    3.     void IResultFilter.OnResultExecuting(ResultExecutingContext filterContext)   
    4.     {  
    5.         filterContext.Controller.ViewBag.OnResultExecuting = "IResultFilter.OnResultExecuting filter called";  
    6.     }  
    7.     void IResultFilter.OnResultExecuted(ResultExecutedContext filterContext)   
    8.     {  
    9.         filterContext.Controller.ViewBag.OnResultExecuted = "IResultFilter.OnResultExecuted filter called";  
    10.     }  
    11. }  
  4. CustomExceptionAttribute.cs 
    1. public class CustomExceptionAttribute: FilterAttribute, IExceptionFilter   
    2. {  
    3.     void IExceptionFilter.OnException(ExceptionContext filterContext)   
    4.     {  
    5.         filterContext.Controller.ViewBag.OnException = "IExceptionFilter.OnException filter called";  
    6.     }  
    7. }  
View
 
Index.cshtml 
  1. @{  
  2. ViewBag.Title = "Index";  
  3. }  
  4. <h2>  
  5. Index</h2>  
  6. <ul>  
  7. <li>@ViewBag.OnAuthorization</li>  
  8. <li>@ViewBag.OnActionExecuting</li>  
  9. <li>@ViewBag.OnActionExecuted</li>  
  10. <li>@ViewBag.OnResultExecuting</li>  
  11. <li>@ViewBag.OnResultExecuted</li>  
  12. <li>@ViewBag.Message</li>  
  13. </ul>  

Here is the Output.

output

Ebook Download
View all
Learn
View all