So let’s start and know about the MVC filters.
Suppose you have to execute some code before and after an action method in an MVC application. So how to overcome this situation? MVC filters are used to deal with this situation.
What are MVC Filters?
In ASP.Net MVC, a filter executes before or after executing an action method.
Basically, Filter in MVC is a custom class where we can write some of our own logic which will execute before or after action method. Hopefully, you must have understood the definition of MVC filters. Now let us know about their type.
Types of MVC Filters
MVC offers 4 types of filters.
1. Authorization Filters
2. Action Filters
3. Result Filters
4. Exception Filters
1. Authorization Filters
Add new class within this folder. Class – AuthFilterAttribute.cs
This class inherits –
1. FilterAttribute class
2. IAuthorizationFilter interface
IExceptionFilter has one method – OnAuthorization
public class AuthFilterAttribute:FilterAttribute, IAuthorizationFilter { public void OnAuthorization(AuthorizationContext filterContext) { filterContext.Controller.ViewBag.Message = “Authorization Filter Executed”; } } |
[AuthFilter] public ActionResult Index() { return View(); } |
<h2>@ViewBag.Message</h2> |
2. Action Filter
This class inherits –
1. FilterAttribute class
2. IActionFilter interface
IExceptionFilter has 2 methods – OnActionExecuting and OnActionExecuted
public class CustomActionFilterAttribute : FilterAttribute, IActionFilter { public void OnActionExecuting(ActionExecutingContext filterContext) { filterContext.Controller.ViewBag.Message = “OnActionExecuting Begins”; } public void OnActionExecuted(ActionExecutedContext filterContext) { filterContext.Controller.ViewBag.Message = “OnActionExecuting Completed and OnActionExecuted Started”; } } |
3. Result Filters
This class inherits-
1. FilterAttribute class
2. IResultFilter interface
IExceptionFilter has 2 methods – OnResultExecuting and OnResultExecuted
public class CustomResultFilter:FilterAttribute, IResultFilter { public void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.Controller.ViewBag.Message2 = “OnResultExecuting”; } public void OnResultExecuted(ResultExecutedContext filterContext) { filterContext.Controller.ViewBag.Message2 = “OnResultExecuted and Done”; } } |
4. Exception Filters
1. FilterAttribute class
2. IExceptionFilter interface
IExceptionFilter has one method – OnException
public class CustomExceptionFilter:FilterAttribute, IExceptionFilter { public void OnException(ExceptionContext filterContext) { filterContext.Controller.ViewBag.Message3 = “Exception Occured”; } } |
You can apply these filters as an attribute on your Action method in Controller class –
public class Default1Controller : Controller { // // GET: /Default1/ [CustomActionFilter] [AuthFilter] [CustomResultFilter] public ActionResult Index() { return View(); } } |
In above sample code, I have stored the message in ViewBag and Printed on View. Instead of storing a message in ViewBag, we can apply some logic that will execute.
<h2>@ViewBag.Message</h2> <h2>@ViewBag.Message1</h2> <h2>@ViewBag.Message2</h2> <h2>@ViewBag.Message3</h2> |
FilterAttribute class inherits Attribute class and IMvcFilter interface
You would love to read below blogs-