hi lets implement asp.net mvc filters
Filters help us to add logic before and after a controller action which is getting called. (Like we need to log something when controller action is called)
types of filters in asp.net mvc :
1. Authorization filter
2. Action filter
3. result filter (viewresult, jsonresult etc.)
4. exception filter
example:
1. Authorization filter
2. Action filter
3. result filter (viewresult, jsonresult etc.)
4. exception filter
example:
create a new asp.net mvc empty project. Now i will add a new controller and will return a string to the view using viewbag.
Now for implementing Custom MVC filters, we will implement methods from IAuthorizationFilter,IActionFilter,IResultFilter,IExceptionFilter
interface.
interface.
controller code
public class MainController : Controller,IAuthorizationFilter,IActionFilter,IResultFilter,IExceptionFilter
{
//
// GET: /Main/
{
//
// GET: /Main/
public ActionResult Index()
{
ViewBag.message = “hello world”;
return View();
}
{
ViewBag.message = “hello world”;
return View();
}
protected override void OnAuthorization(AuthorizationContext filterContext)
{
ViewBag.AuthorizationMsg = “From OnAuthorization method”;
}
{
ViewBag.AuthorizationMsg = “From OnAuthorization method”;
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
ViewBag.ActionExecutingMsg = “From OnActionExecuting method”;
}
{
ViewBag.ActionExecutingMsg = “From OnActionExecuting method”;
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
ViewBag.ActionExecutedMsg = “From OnActionExecuted method”;
}
{
ViewBag.ActionExecutedMsg = “From OnActionExecuted method”;
}
protected override void OnResultExecuting(ResultExecutingContext filterContext)
{
ViewBag.ResultExecutingMsg = “From OnResultExecuting method”;
}
{
ViewBag.ResultExecutingMsg = “From OnResultExecuting method”;
}
protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
ViewBag.ResultExecutedMsg = “From OnResultExecuted method”;
}
{
ViewBag.ResultExecutedMsg = “From OnResultExecuted method”;
}
protected override void OnException(ExceptionContext filterContext)
{
ViewBag.ExceptionMsg = “From OnException method”;
}
{
ViewBag.ExceptionMsg = “From OnException method”;
}
}
View code
<!DOCTYPE html>
<html>
<head>
<meta name=”viewport” content=”width=device-width” />
<title>Index</title>
</head>
<body>
<div>
@ViewBag.message
<head>
<meta name=”viewport” content=”width=device-width” />
<title>Index</title>
</head>
<body>
<div>
@ViewBag.message
<br />
@ViewBag.AuthorizationMsg
<br />
@ViewBag.ActionExecutingMsg
<br />
@ViewBag.ActionExecutedMsg
<br />
@ViewBag.ResultExecutingMsg
<br />
@ViewBag.ResultExecutedMsg
<br />
@ViewBag.ExceptionMsg
</div>
</body>
</html>
@ViewBag.AuthorizationMsg
<br />
@ViewBag.ActionExecutingMsg
<br />
@ViewBag.ActionExecutedMsg
<br />
@ViewBag.ResultExecutingMsg
<br />
@ViewBag.ResultExecutedMsg
<br />
@ViewBag.ExceptionMsg
</div>
</body>
</html>
Output :
Below is the sequence of execution of the methods:
As View gets rendered before OnResultExecuted method , we are not able to see the onresultexecuted viewbag message.
OnException method will get called when there is any exception.
No comments:
Post a Comment