MVC Action Filter


Introduction

In this blog we are going to discuss the Action Filter in MVC, we will discuss the purpose and how to create Action Filter.

Getting Started

Action Filter performs some operation before and after an action method executes means Action Filter help us to perform logic while an MVC action is executing or after an MVC action has executed. An action filter can be applied to either an individual controller action or an entire controller. Action filters are useful in the following scenarios.

  1. Implement post-processing logic before the action happens.
  2. Cancel a current execution.
  3. Inspect the returned value.
  4. Provide extra data to the action.
Type of Action Filter
  1. Inline Action Filter.
  2. Attribute Action Filter.
Inline Action Filter

To create an inline action attribute we need to implement the IActionFilter interface in controller. The IActionFilter interface has two Methods that is

  1. OnActionExecuted
  2. OnActionExecuting
.
We can implement pre-processing or cancellation logic and post processing logic in these methods.

 public class HomeController : Controller , IActionFilter  
 {  
   public ActionResult Index(Customer obj)  
   {  
     return View(obj);  
   }  
   void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)  
   {  
     Trace.WriteLine("Action Executed");  
   }  
   void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)  
   {  
     Trace.WriteLine("Action is executing");  
   }  
 }  

The above code creates an inline Action Filter but it has problem, the problem is that it cannot be reused across controllers, it cannot be used in multiple controller. To short out this problem MVC introduced the Attribute Action Filter.

Attribute Action Filter

To create an action filter attribute we need to inherit from ActionFilterAttribute abstract class and implement the IActionFilter interface as shown in the below code.

Follow the below steps to create attribute action filter in your project. Note that you must have idea about MVC project creation before going to the steps.

Steps :-
  1. Create MVC Project or open your existing MVC Project in which you want to add attribute action filter.
  2. Create a new folder inside the MVC project and name is as Filters.
  3. Right on the folder add a new class having name “MyActionFilterAttribute
  4. Inherit the ActionFilterAttribute class.
  5. Implement the IActionFilter interface and the methods of IActionFilter interface.
  6. Now you are done with the creation of attribute ActionFilter and your newly created attribute class should look like below code.
 public class MyActionFilterAttribute : ActionFilterAttribute , IActionFilter  
 {  
  void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)  
  {  
  Trace.WriteLine("Action Executed");  
  }  
  void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)  
  {  
  Trace.WriteLine("Action executing");  
  }  
 }  

Now we can decorate the controllers on which we want the action attribute to execute. You can see in the below code I have decorated the HomeController with the MyActionAttribute class which was created in the previous code.

 [MyActionFilterAttribute ]  
 public class MyController : Controller   
 {  
   public ActionResult Index()  
   {  
     return View();  
   }  
 }  

Related Articles

  1. Various Filters in MVC
  2. MVC Exception Filter
  3. MVC Authorization Filters

Summary

In this blog we discussed what is Action filter, how it is working and how to create Action filter, hope you enjoyed.

Thanks