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");  
   }  
 }  

Example of IActionFilter in MVC

The above code creates an inline Action Filter but it has a problem, the problem is that it cannot be reused across controllers, it cannot be used in multiple controllers. 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");  
  }  
 }  

Example of custom ActionFilter attribute in MVC

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();  
   }  
 }  

Use of custom ActionFilter in MVC

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

Kailash Chandra Behera

An IT Professional with 12 years experience in development life cycle in windows, service and Web based application using Microsoft.Net technologies. Proven record of developing all phases of projects in Microsoft.Net technology from initiation to closure aligning with the company's Business objectives to drive process improvements, competitive advantage and bottom-line gains. -> Good exposure of independently working and developing multiple projects ->Committed to efficient and effective development of projects in a fast-paced and deadline driver environment. Skill :- Develop and design projects in various technologies of Microsoft Technology. Total IT Experience- 13+

Previous Post Next Post

نموذج الاتصال