Asp.Net MVC Filters (Action, Result, Authorization, Exception)

Here we will learn filters in asp.net mvc with examples and different types of action filters in asp.net mvc with examples.

Asp.Net MVC Action Filters

Generally, sometimes we want to check some logic before an action method is called or after an action method is executed. To support this kind of situation, ASP.NET MVC provides action filters. Action Filters are the attributes that can apply to the action method or controller to perform logic either before an action method is called or after an action method is executed.

 

In asp.net mvc, we have a different types of action filters available those are

 

FiltersDescription
Authorization Filters These filters are used to implement the IAuthorizationFilter attribute.
Action Filters These are used to implement the IActionFilter attribute.
Result Filters These are used to implement the IResultFilter attribute.
Exception Filters These are used to implement the IExceptionFilter attribute.

Authorization Filters in Asp.Net MVC

Authorization filters are used to authenticate whether the user requested action method in the controller is authorized to access or not and for validating properties of the request. Authorization filters run before any other filter. Generally, we will use authorization filters like as shown 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 = new ViewResult();

result.ViewName = "Error";

result.ViewBag.ErrorMessage = "Invalid User";

filterContext.Result = result;

}

}

}

}

}

Here in the above code snippet, we created an Authorization filters Attribute. We created a class and inherited from FilterAttribute, IAuthorizationFilter classes, and implemented the OnAuthorization method 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 been executed.  It has two methods.

 

  • OnActionExecuted.
  • OnActionExecuting.

Following is the sample code snippet to use action filters in asp.net mvc application. 

 

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 = new RedirectResult("/Home/Index");

}

else

{

filterContext.Result = new RedirectResult("/Login /Login");

}

}

public void OnActionExecuting(ActionExecutingContext filterContext)

{

if (filterContext.HttpContext.Session["UserID"] != null)

{

filterContext.Result = new RedirectResult("/Home/ Index");

}

else

{

filterContext.Result = new RedirectResult("/Login /Login");

}

}

}

}

[Myactionfilter]

[AcceptVerbs(HttpVerbs.Get)]

public ActionResult GetPerson()

{

Person p = new Person ();

return View("Person",p);

}

Here we used this method to check whether the Session of UserID, which is null or not. If it’s null, we are sending it to login else to Home.

Result Filters in Asp.Net MVC

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

 

  • OnResultExecuted
  • OnResultExecuting

Following is the sample code snippet to use result filters in the asp.net mvc application.

 

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 = new RedirectResult("/Home/Contact");

}

else

{

filterContext.Result = new RedirectResult("/Login/Login");

}

}

public void OnResultExecuting(ResultExecutingContext filterContext)

{

if (filterContext.HttpContext.Session["UserID"] != null)

{

filterContext.Result = new RedirectResult("/Home/Contact");

}

else

{

filterContext.Result = new RedirectResult("/Login/Login");

}

}

}

}

[MyResultfilter]

[AcceptVerbs(HttpVerbs.Get)]

public ActionResult GetPerson()

{

Person p = new Person ();

return View("Person",p);

}

Exception Filters in Asp.Net MVC

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

 

Following is the sample code snippet to use exception filters in the asp.net mvc application.

 

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 = new Person ();

return View("Person",p);

}

}

 We can use different action filters in asp.net mvc applications based on our requirements.