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

  Total Views : 3273
  Comments : 0
 
  Keywords : Action filters in asp.net mvc, Types of action filters in asp.net mvc, Authorization filters in asp.net mvc, Exception filters in asp.net mvc, Result filters in asp.net mvc

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 an action filters. The Action Filters are the attributes that can be applied on action method or controller to perform logic either before an action method is called or after an action method executed.

 

In asp.net mvc we have different type of action filters avaliable those are

 

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

Authorization Filters in Asp.Net MVC

Authorization filters are used to authenticate whether user requested action method in controller is authorized to access or not and also 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 = newViewResult();

result.ViewName = "Error";

result.ViewBag.ErrorMessage = "Invalid User";

filterContext.Result = result;

}

}

}

}

}

Here in above code snippet we created an Authorization filters Attribute for that we created a class and inherited to class FilterAttribute, IAuthorizationFilter to it and implemented OnAuthorization method of it to write our custom logic inside it.

Action Filters in Asp.Net MVC

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

 

  • OnActionExecuted.
  • OnActionExecuting.

Following is the sample code sinppet to use action filters in asp.net mvc applicaiton 

 

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);

}

Here 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

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 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 = 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

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

return View("Person",p);

}

}

 We can use different type of action filters in asp.net mvc applicaiton based on our requirement.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User