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.
- Implement post-processing logic before the action happens.
- Cancel a current execution.
- Inspect the returned value.
- Provide extra data to the action.
- Inline Action Filter.
- Attribute 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
- OnActionExecuted
- 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.
- Create MVC Project or open your existing MVC Project in which you want to add attribute action filter.
- Create a new folder inside the MVC project and name is as Filters.
- Right on the folder add a new class having name “MyActionFilterAttribute”
- Inherit the ActionFilterAttribute class.
- Implement the IActionFilter interface and the methods of IActionFilter interface.
- 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
Summary
In this blog we discussed what is Action filter, how it is working and how to create Action filter, hope you enjoyed.
Thanks