please tell me or explain to me the different types of action filters used in asp.net MVC with a suitable example.
Explain different Filters in asp.net MVC?
787
04-Feb-2020
Updated on 04-Feb-2020
Nishi Tiwari
04-Feb-2020Asp.Net 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 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
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
The simple code sinppet to use action filters in asp.net mvc application are given below:-
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.