Understanding ASP.Net MVC Filters

In this blog we will see about MVC filters. So what are filters in MVC and where can we use them in our application.

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

Authorization filter in MVC implements IAuthorizationFilter interface
Executes before an action method to check the authenticity of a user which is going to execute the specific action method.
Whenever a request comes from the browser,  IAuthorizationFilter.OnAuthorization method executes and then goes to ActionFilter.

Create a folder in solution and name it Custom.
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

Action filters in MVC implements IActionFilter interface
IActionFilter.OnActionExecuting method executes before an action method gets executed and once action method executed IActionFilter.OnActionExecuted method gets invoked.
Then request goes to Result 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 filter in MVC implements IResultFilter interface
Before executing the result IResultFilter.OnResultExecuting methods called and after executing the result, IResultFilter.OnResultExecuted method gets invoked.

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

Exception filter in MVC implements IExceptionFilter interface
Executes when an action throws an exception in between any stage of execution.
 This class inherits –

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

 
Hope you understand the concept of MVC filters. If this is useful to you then please share this blog within your community.

You would love to read below blogs-

Leave a Comment

RSS
YouTube
YouTube
Instagram