Understanding ASP.Net 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.

MVC offers 4 types of filters.

1. Authorization Filters
2. Action Filters
3. Result Filters
4. Exception Filters

1. Authorization Filters 

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

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

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

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

You would love to read below blogs-



Comments

Popular

What's new in C# 6.0 language - All new features of C# 6.0 for developers

SharePoint Interview Questions and Answers