Action Filters In ASP.NET MVC

Action Filters In ASP.NET MVC are custom classes with whom you can write custom logic before or after execution of Controller action methods. Filters can be applied as attribute or as implementing their respecting interface.

Action Filters

Filters are custom classes with whom you can write custom logic before or after execution of Controller action methods. Filters can be applied as attribute or as implementing their respecting interface.

Now, ASP.NET Action Filters are custom classed where you can write your custom logic. These can be executed before or after action method execution.

ASP.NET MVC provides different types of filters which are provided below.

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

Authorization Filters

It enables you to restrict access to particular user or role. You can achieve this by implementing IAuthorizationFilter.

Below, a sample of implantation IAuthorizationFilter is provided.

  1. public class CustomAuthorizationAttribute: FilterAttribute, IAuthorizationFilter {  
  2.     void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext) {  
  3.         filterContext.Controller.ViewBag.OnAuthorization = " OnAuthorization of IAuthorizationFilter filter called";  
  4.     }  
  5. }   

Exception Filters

It enables exception handling when a Controller or action is executed. You can achieve this by implementing IExceptionFilter. 

  1. public class CustomExceptionAttribute: FilterAttribute, IExceptionFilter {  
  2.     void IExceptionFilter.OnException(ExceptionContext filterContext) {  
  3.         filterContext.Controller.ViewBag.OnException = "OnException of IExceptionFilter filter called";  
  4.     }  
  5. }   

Action Filters

It enables you to execute custom logic before and after execution of action method. You can put pre-processing and post – processing logic. This can be achieved by implementing IActionFilter.

Below is sample of implantation IActionFilter is provided, 

  1. public class CustomActionAttribute: FilterAttribute, IActionFilter {  
  2.     void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext) {  
  3.         filterContext.Controller.ViewBag.OnActionExecuted = " OnActionExecuted of IActionFilter filter called";  
  4.     }  
  5.     void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) {  
  6.         filterContext.Controller.ViewBag.OnActionExecuting = " OnActionExecuting of IActionFilter filter called";  
  7.     }  
  8. }   

Result Filters

It enables you to modify the result, before and after execution of result of action method. This can be achieved by implementing IResultFilter.

Below is a sample of implantation IResultFilter. 

  1. public class CustomResultAttribute: FilterAttribute, IResultFilter {  
  2.     void IResultFilter.OnResultExecuted(ResultExecutedContext filterContext) {  
  3.         filterContext.Controller.ViewBag.OnResultExecuted = "OnResultExecuted of IResultFilter filter called";  
  4.     }  
  5.     void IResultFilter.OnResultExecuting(ResultExecutingContext filterContext) {  
  6.         filterContext.Controller.ViewBag.OnResultExecuting = "OnResultExecuting of IResultFilter filter called";  
  7.     }  
  8. }