Implementing Custom Action Filters in MVC
Action filter is an attribute to an action in MVC. The action filters is used in the scenarios like if you want to run some specific logic before or after an action method runs . In order to achieve this kind of functionality we can use action filters. In other words action filter is attribute to an action that helps us to modify the way in which the action execute .
Action filter is an attribute to an action in MVC. The action filters is used in the scenarios like if you want to run some specific logic before or after an action method runs . In order to achieve this kind of functionality we can use action filters. In other words action filter is attribute to an action that helps us to modify the way in which the action execute .
Types of Action filters
- OutPutCache - Caches the output of a controller action for a certain period of time.
- Handle Error - Handles the error raised when an controller action executes .
- Autherise - This enables to autherise to a particular user roles .
- Custome Filters - We can also create different custom filters so that our custom logic should run before /after the controller action executes .
How Can we user action filters in MVC ?
We can use action filters in MVC as
Attributes . We can apply action filters mainly 2 types.
Apply for individual action with in a controller
public
class
HomeController
: Controller
{
[OutputCache(Duration
= 10)]
public
string
test()
{
return
DateTime.Now.ToString();
}
}
or we can apply it for all actions in a
controller.
[OutputCache(Duration
= 10)]
public
class
HomeController
: Controller
{
public
string
test()
{
return
DateTime.Now.ToString();
}
}
ActionFilterAttribute Class
The MVC Framework included a base
class called ActionFilterAttribute class in order to implement the
custom action filters . The ActionFilterAttributeclass implemets
both IActionFilter and IResultFilter and inherited from Filter Class
. Basically when ever you are creating a custom action filter by
inheriting ActionFilterAttribute class it is both Action filter and
Result filter.
In ActionFilterAttribute Class we can
override the following methods
- OnActionExecuting – Called before controller action is executed.
- OnActionExecuted – Called after a controller action is executed.
- OnResultExecuting – Called before controller action result is executed.
- OnResultExecuted – Called after a controller action result is executed .
I have created a Custom filter class
and inherit the ActionFilterAttribute class
and called it as attribute in two
controller action 'register' and 'forgotpassword'
My filter class looks like below
public
class
TestActionFilter
: ActionFilterAttribute
{
public
override
void
OnActionExecuting(ActionExecutingContext
filterContext)
{
LogFilterSequence("OnActionExecuting",
filterContext.RouteData);
}
public
override
void
OnActionExecuted(ActionExecutedContext
filterContext)
{
LogFilterSequence("OnActionExecuted",
filterContext.RouteData);
}
public
override
void
OnResultExecuting(ResultExecutingContext
filterContext)
{
LogFilterSequence("OnResultExecuting",
filterContext.RouteData);
}
public
override
void
OnResultExecuted(ResultExecutedContext
filterContext)
{
LogFilterSequence("OnResultExecuted",
filterContext.RouteData);
}
private
void
LogFilterSequence(string
methodName, RouteData
routeData)
{
var
controllerName = routeData.Values["controller"];
var
actionName = routeData.Values["action"];
var
message = String.Format("{0}
controller:{1} action:{2}",
methodName, controllerName, actionName);
Debug.WriteLine(message,
"Custome
Action filter sequence ");
}
}
I have called this filter attribute to
my controller action
[TestActionFilter]
public
ActionResult
register()
{
return
View();
}
[TestActionFilter]
public
ActionResult
ForgotPassword()
{
return
View();
}
The result will be as below.
Custom Action filter sequence : OnActionExecuting controller:Home
action:register
Custom Action filter sequence : OnActionExecuted controller:Home
action:register
Custom Action filter sequence : OnResultExecuting controller:Home
action:register
Custom Action filter sequence : OnResultExecuted controller:Home
action:register
Custom Action filter sequence : OnActionExecuting controller:Home
action:forgotpassword
Custom Action filter sequence : OnActionExecuted controller:Home
action:forgotpassword
Custom Action filter sequence : OnResultExecuting controller:Home
action:forgotpassword
Custom Action filter sequence : OnResultExecuted controller:Home
action:forgotpassword
No comments:
Post a Comment