Action Filter will help you to perform extra oprations while MVC Controller or Action before Executing or After Executed. We can check authorize user by using "IAuthorizationFilter" interface. Exception handling is also managed by "IExceptionFilter" interface. we can also call Action Filter from global file register with below code.
Go to Global.asax file
Register Filter at application level.
Go to Filter.config in App_Start folder.
Add Custom Filter here filter.config file
ActionFilterAttribute represents the base class of filter attributes.which is contains The filter context as below
this method will call after the action method method executes.
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
}
this method will call after the action result executes.
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
base.OnResultExecuted(filterContext);
}
this method will call before the action method executes.
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
}
this method will call before the action result executes.
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);
}
Later on we can use in the Controller on which action attribute to execute.
Call Filter on top of the controller as below
[Authorize]
public class UserController : Controller
{
}
Call Filter on top of the Action as below
public class UserController : Controller
{
[Authorize]
public ActionResult Login(string username)
{
// Logic
return View();
}
}
I hope you like this post. If you have any query regarding Action filters then you can post your feedback or send me email at mehulpatelk152@gmail.com.




No comments:
Post a Comment