Filters in ASP.NET MVC are a way to apply cross-cutting logic at the controller level.
ASP.NET MVC provides Action Filters for executing filtering logic either before or after an action method is called. Action Filters are custom attributes that provide declarative means to add pre-action and post-action behavior to the controller's action methods.
public class CustomAuthenticationAttribute : FilterAttribute, IAuthenticationFilter
{
void IAuthenticationFilter.OnAuthentication(AuthenticationContext filterContext)
{
filterContext.Controller.ViewBag.OnAuthentication = "IAuthenticationFilter.OnAuthentication filter called";
}
void IAuthenticationFilter.OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
{
filterContext.Controller.ViewBag.OnAuthenticationChallenge = "IAuthenticationFilter.OnAuthenticationChallenge filter called";
}
}
******************************************************************
public class CustomAuthorizationAttribute : FilterAttribute, IAuthorizationFilter
{
void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)
{
filterContext.Controller.ViewBag.OnAuthorization = "IAuthorizationFilter.OnAuthorization filter called";
}
}
*********************************************************************
void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.Controller.ViewBag.OnActionExecuted = "IActionFilter.OnActionExecuted filter called";
}
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewBag.OnActionExecuting = "IActionFilter.OnActionExecuting filter called";
}
***************************************************************************
public class CustomResultAttribute : FilterAttribute, IResultFilter
{
void IResultFilter.OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.Controller.ViewBag.OnResultExecuted = "IResultFilter.OnResultExecuted filter called";
}
void IResultFilter.OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.Controller.ViewBag.OnResultExecuting = "IResultFilter.OnResultExecuting filter called";
}
}
********************************************************
public class CustomExceptionAttribute : FilterAttribute, IExceptionFilter
{
void IExceptionFilter.OnException(ExceptionContext filterContext)
{
filterContext.Controller.ViewBag.OnException = "IExceptionFilter.OnException filter called";
}
}
****************************************************************
Now create a controller
public class HomeController : Controller
{
[CustomAuthenticationAttribute]
[CustomAuthorization]
[CustomAction]
[CustomResultAttribute]
[CustomExceptionAttribute]
public ActionResult Index()
{
// throw new Exception("Dummy Exception");
ViewBag.Message = "Index Action of Home controller is being called.";
return View();
}
}
*********************************************
Below is the View
@{
ViewBag.Title = "Index";
}
<h2>
Index
</h2>
<ul>
<li>@ViewBag.OnAuthentication</li>
<li>@ViewBag.OnAuthorization</li>
<li>@ViewBag.OnActionExecuting</li>
<li>@ViewBag.OnActionExecuted</li>
<li>@ViewBag.OnAuthenticationChallenge</li>
<li>@ViewBag.OnResultExecuting</li>
<li>@ViewBag.OnResultExecuted</li>
</ul>
ASP.NET MVC provides Action Filters for executing filtering logic either before or after an action method is called. Action Filters are custom attributes that provide declarative means to add pre-action and post-action behavior to the controller's action methods.
public class CustomAuthenticationAttribute : FilterAttribute, IAuthenticationFilter
{
void IAuthenticationFilter.OnAuthentication(AuthenticationContext filterContext)
{
filterContext.Controller.ViewBag.OnAuthentication = "IAuthenticationFilter.OnAuthentication filter called";
}
void IAuthenticationFilter.OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
{
filterContext.Controller.ViewBag.OnAuthenticationChallenge = "IAuthenticationFilter.OnAuthenticationChallenge filter called";
}
}
******************************************************************
public class CustomAuthorizationAttribute : FilterAttribute, IAuthorizationFilter
{
void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)
{
filterContext.Controller.ViewBag.OnAuthorization = "IAuthorizationFilter.OnAuthorization filter called";
}
}
*********************************************************************
void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.Controller.ViewBag.OnActionExecuted = "IActionFilter.OnActionExecuted filter called";
}
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewBag.OnActionExecuting = "IActionFilter.OnActionExecuting filter called";
}
***************************************************************************
public class CustomResultAttribute : FilterAttribute, IResultFilter
{
void IResultFilter.OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.Controller.ViewBag.OnResultExecuted = "IResultFilter.OnResultExecuted filter called";
}
void IResultFilter.OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.Controller.ViewBag.OnResultExecuting = "IResultFilter.OnResultExecuting filter called";
}
}
********************************************************
public class CustomExceptionAttribute : FilterAttribute, IExceptionFilter
{
void IExceptionFilter.OnException(ExceptionContext filterContext)
{
filterContext.Controller.ViewBag.OnException = "IExceptionFilter.OnException filter called";
}
}
****************************************************************
Now create a controller
public class HomeController : Controller
{
[CustomAuthenticationAttribute]
[CustomAuthorization]
[CustomAction]
[CustomResultAttribute]
[CustomExceptionAttribute]
public ActionResult Index()
{
// throw new Exception("Dummy Exception");
ViewBag.Message = "Index Action of Home controller is being called.";
return View();
}
}
*********************************************
Below is the View
@{
ViewBag.Title = "Index";
}
<h2>
Index
</h2>
<ul>
<li>@ViewBag.OnAuthentication</li>
<li>@ViewBag.OnAuthorization</li>
<li>@ViewBag.OnActionExecuting</li>
<li>@ViewBag.OnActionExecuted</li>
<li>@ViewBag.OnAuthenticationChallenge</li>
<li>@ViewBag.OnResultExecuting</li>
<li>@ViewBag.OnResultExecuted</li>
</ul>
*****************************
Now check the order
- IAuthenticationFilter.OnAuthentication
- IAuthorizationFilter.OnAuthorization
- IActionFilter.OnActionExecuting
- public ActionResult Index() {}
- void IActionFilter.OnActionExecuted
- IAuthenticationFilter.OnAuthenticationChallenge
- IResultFilter.OnResultExecuting
- IResultFilter.OnResultExecuted
************************************************
Index
- IAuthenticationFilter.OnAuthentication filter called
- IAuthorizationFilter.OnAuthorization filter called
- IActionFilter.OnActionExecuting filter called
- IActionFilter.OnActionExecuted filter called
- IAuthenticationFilter.OnAuthenticationChallenge filter called
- IResultFilter.OnResultExecuting filter called
- ************************************************************
Now throw exception
Now create a controller
public class HomeController : Controller
{
[CustomAuthenticationAttribute]
[CustomAuthorization]
[CustomAction]
[CustomResultAttribute]
[CustomExceptionAttribute]
public ActionResult Index()
{
throw new Exception("Dummy Exception");
ViewBag.Message = "Index Action of Home controller is being called.";
return View();
}
}
public class HomeController : Controller
{
[CustomAuthenticationAttribute]
[CustomAuthorization]
[CustomAction]
[CustomResultAttribute]
[CustomExceptionAttribute]
public ActionResult Index()
{
throw new Exception("Dummy Exception");
ViewBag.Message = "Index Action of Home controller is being called.";
return View();
}
}
- IAuthenticationFilter.OnAuthentication
- IAuthorizationFilter.OnAuthorization
- IActionFilter.OnActionExecuting
- public ActionResult Index(){}
- IActionFilter.OnActionExecuted
- IExceptionFilter.OnException
Then Yellow Screen of Death