Custom
Filters:
Before implementing any
custom filter don’t forget to define the namespace for filters i.e.
“System.Web.Mvc”. Create a folder named “AppAuth” (any name) under your MVC
Project and create the custom filter classes under that folder. Implement
corresponding interfaces and Attribute Classes for creating Custom filters.
1.Custom
Authorization Filter :
This filter
will be executed once after user is authenticated. Create
a class under a “AppAuth” folder named “CustAuthFilter.cs” and place the
codes as below.
public class CustAuthFilter : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
filterContext.Controller.ViewBag.AutherizationMsg = "Custom
Authorization:
Message from OnAuthorization method.";
}
}
2.Custom
Action Filter :
Create
a class under a “AppAuth” folder named “CustActionFilter.cs” and place the
codes as below. There are 4 events available in an action filter.
·
OnActionExecuting - Runs before execution
of Action method.
·
OnActionExecuted - Runs after execution of
Action method.
·
OnResultExecuting - Runs before content is
rendered to View.
·
OnResultExecuted - Runs after content is
rendered to view.
public
class CustomActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewBag.CustomActionMsgOne = "Custom Action Filter: Message from OnActionExecuting method.";
}
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.Controller.ViewBag.CustomActionMsgTwo = "Custom Action
Filter: Message from OnActionExecuted method.";
}
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.Controller.ViewBag.CustomActionMsgThree= "Custom Action Filter:
Message from OnResultExecuting method.";
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.Controller.ViewBag.CustomActionMsgFour= "Custom Action
Filter:
Message from OnResultExecuted method."
}
}
3.Custom Exception Filter:
This filter is
used to capture any exceptions if raised by controller or an action method. Create class under a “AppAuth”
folder
named “CustExceptionFilter.cs” and place the
codes as below.
public class CustExceptionFilter : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
filterContext.Controller.ViewBag.ExceptionMsg
= "Custom
Exception Message from
OnException method.";
}
}
How To use the Custom
filters in the Controller :
Create a Controller named “CustomActionFilterController” under the Controllers folder. Then decorate the
controller with your custom filters as following.
using System.Web.Mvc;
using
MvcCustomFilter.AppAuth;
namespace
MvcCustomFilter.Controllers
{
[CustAuthFilter]
public class CustomActionFilterController : Controller
{
[CustActionFilter]
[CustExceptionFilter]
public ActionResult Index()
{
ViewBag.Message = "Here is Your
Custom Filters. !";
return View();
}
public ActionResult About()
{
return View();
}
}
}
Now Create a view for the “Index” action as
follows inside “Index.cshtml” :
@{
ViewBag.Title = "Index";
}
<h2>:Custom
Filters:</h2>
<h3>@ViewBag.Message</h3><br />
<h2>Output Messages from
Controller :</h2><br />
<h3>@ViewBag.AutherizationMsg</h3><br />
<h3>@ViewBag.CustomActionMsgOne</h3><br />
<h3>@ViewBag.CustomActionMsgTwo</h3><br />
<h3>@ViewBag.CustomActionMsgThree</h3><br />
<h3>@ViewBag.CustomActionMsgFour</h3><br />
<h3>@ViewBag.ExceptionMsg</h3>
No comments:
Post a Comment