---
title: "Explain different Filters in asp.net MVC?"  
description: "Explain different Filters in asp.net MVC?"  
author: "Anonymous User"  
published: 2020-02-04  
updated: 2020-02-04  
canonical: https://www.mindstick.com/forum/155780/explain-different-filters-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 4 minutes  

---

# Explain different Filters in asp.net MVC?

[please tell me](https://www.mindstick.com/forum/33900/please-tell-me-what-are-the-technique-to-content-optimization) or [explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) to me the different types of action filters used in [asp.net MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) with a suitable example.

## Replies

### Reply by Nishi Tiwari

**[Asp.Net](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc) Action Filters**

If sometimes we want to check some logic before an action method is called or after an action method is executed. So to support this kind of situation ASP.NET MVC provides an action filter. **[These Action Filters are the attributes](https://www.mindstick.com/forum/155779/define-action-verbs-in-asp-dot-net-mvc-with-an-example)** which can be applied to the action method or the controller so to perform logic either before an action method is called or after an action method executed.

We have a different type of action filters are available in asp.net MVC

## Filters

Authorization Filters:-It is used to implement the IAuthorizationFilter attribute.

Action Filters:- It is used to implement the IActionFilter attribute.

Result Filters It is used to implement the IResultFilter attribute.

Exception Filters It is used to implement the IExceptionFilter attribute.

## Authorization Filters in Asp.Net MVC

The authorization filters are used to authenticate whether the user requested action method in the controller is authorized to access or not and also used for validating properties of the request. Authorization filters run before any other filter. We use authorization filters as given below

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Tutorial4.Filters
{
public class Myfilter : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAuthenticated)
{
if (!Roles.IsUserInRole("Admin"))
{
ViewResult result = newViewResult();
result.ViewName = "Error";
result.ViewBag.ErrorMessage = "Invalid User";
filterContext.Result = result;
}
}
}
}
}
```

In the above code snippet, we had created an Authorization filters Attribute for which we created a class and inherited to class FilterAttribute, IAuthorizationFilter to it and implemented OnAuthorization method of it so to write our custom logic inside it.

## Action Filters in Asp.Net MVC

Action filters are called before executing the Action Method and after the Action Method has executed. It has two methods

- OnActionExecuted.
- OnActionExecuting.

The simple code sinppet to use action filters in asp.net mvc application are given below:-

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Tutorial4.Filters
{
public class Myactionfilter : FilterAttribute,IActionFilter
{
public void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.HttpContext.Session["UserID"] != null)
{

filterContext.Result = newRedirectResult("/Home/Index");
}
else
{
filterContext.Result = newRedirectResult("/Login /Login");
}
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Session["UserID"] != null)
{
filterContext.Result = newRedirectResult("/Home/ Index");
}
else
{
filterContext.Result = newRedirectResult("/Login /Login");
}
}
}
}
[Myactionfilter]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetPerson()
{
Person p = newPerson ();
return View("Person",p);
}
```

In the above code, we used this method to check whether Session of UserID which is null or not if it’s null then we are sending it to login else to Home.

## Result Filters in Asp.Net MVC

This filter will be called before or after generating the result for an Action Method. It has two methods

• OnResultExecuted

• OnResultExecuting

The simple code snippet to use result filters in asp.net mvc application are given below:-

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Tutorial4.Filters
{
public class MyResultfilter : FilterAttribute,IResultFilter
{
public void OnResultExecuted(ResultExecutedContext filterContext)
{
if (filterContext.HttpContext.Session["UserID"] != null)
{
filterContext.Result = newRedirectResult("/Home/Contact");
}
else
{
filterContext.Result = newRedirectResult("/Login/Login");
}
}
public void OnResultExecuting(ResultExecutingContext filterContext)
{
if (filterContext.HttpContext.Session["UserID"] != null)
{
filterContext.Result = newRedirectResult("/Home/Contact");
}
else
{
filterContext.Result = newRedirectResult("/Login/Login");
}
}
}
}
[MyResultfilter]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetPerson()
{
Person p = newPerson ();
return View("Person",p);
}
```

## Exception Filters in Asp.Net MVC

The exception filters will be called whenever a controller or action method of the controller throws an exception. The exception filter will be useful when we need error logging.

Following is the simple code snippet to use exception filters in asp.net mvc application as shown below

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Tutorial4.Filters
{
public class MyExceptionFilter : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
filterContext.Controller.ViewBag.onExceptionError = "ExceptionFilter filter called";
filterContext.HttpContext.Response.Write("ExceptionFilter filter called");
}
}
}
[MyExceptionFilter]
public class HomeController : Controller
{
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetPerson()
{
Person p = newPerson ();
return View("Person",p);
}
}
```

So, we can use different type of action filters in asp.net mvc applicaiton based upon our requirement.

![Explain different Filters in asp.net MVC?](https://www.mindstick.com/mindstickforums/a86efa27-2d19-497a-ab36-25b6afd72720/images/0ed6440f-e5b7-4bed-81cb-3db18a7bfdd2.png)\


---

Original Source: https://www.mindstick.com/forum/155780/explain-different-filters-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
