We already know that Action Filters are important to modify the behavior of the action. If we want to apply some operations on the action execution then we need to make our own custom action filters which help us to apply the operations on different states. We’ll discuss them in detail here.
Let’s create the custom action filter which will log (print/show) the following details.
- Name of the Controller
- Name of the Action
- Execution Time of the Action Method
- And if there is any error or an exception in the action method, then it will log that exception message and the time of the exception when it occurs.
We have already seen the order of execution of the action filters. But if we want to execute them in a specific order, then we’ll use the Order property of action filter.
Remember
The base class type of all the custom action filters is FilterAttribute and if you want to create your own custom action filter then you need to inherit this class from the base FilterAttribute class and we’ve already seen our different types of action filters actually implement relevant specific interfaces.
- Authorization Filter (implements IAuthorizationFilter)
- Action Filter (implements IActionFilter)
- Result Filter (implements IResultFilter)
- Exception Filter (implements IExceptionFilter)
So we need to implement one of these interfaces as well which depends upon our requirement. If we’re making an authorization filter then we’ll implement IAuthorizationFilter interface and if we’re developing Action Filter then we’ll implement IActionFilter and so on.
But if you want to make Action Filter then you’ve another option as well, make your custom class and just inherit it from ActionFilterAttribute.
Let’s start our journey.
- Make an ASP.NET application [Empty or MVC (depends upon your choice)]
- Now add the folder here ‘ActionFilters’
- Add class ‘LogTimeException’
- And inherit it from FilterAttribute and IActionFilter
- namespace SCMS.ActionFilters
- {
- public class LogTimeException: FilterAttribute, IActionFilter
- {
- public void OnActionExecuting(ActionExecutingContext filterContext)
- {
- throw new System.NotImplementedException();
- }
- public void OnActionExecuted(ActionExecutedContext filterContext)
- {
- throw new System.NotImplementedException();
- }
- }
- }
This interface implements 2 methods. We’ve four interfaces (IAuthorizationFilter, IActionFilter and so on) and all of them have different number of abstract methods in it. For more details,
- OnActionExecuting()
It executes before the action is executed. - OnActionExecuted()
It executes after the action is executed.
And if we remove these 2 methods and inherit our class just with ActionFilterAttribute then it overrides 4 methods.
- OnActionExecuting()
- OnActionExecuted()
- OnResultExecuting()
It executes before the result executes meanings that before rendering the view
- OnResultExecuted()
It executes after the result executes means that after rendering the view.
Plus we need to show the exception messages as well so implement IExceptionFilter.
Now this is what our custom action filter class looks like,
- public class LogTimeException : ActionFilterAttribute, IExceptionFilter
- {
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- base.OnActionExecuting(filterContext);
- }
- public override void OnActionExecuted(ActionExecutedContext filterContext)
- {
- base.OnActionExecuted(filterContext);
- }
- public override void OnResultExecuting(ResultExecutingContext filterContext)
- {
- base.OnResultExecuting(filterContext);
- }
- public override void OnResultExecuted(ResultExecutedContext filterContext)
- {
- base.OnResultExecuted(filterContext);
- }
- public void OnException(ExceptionContext filterContext)
- {
- throw new System.NotImplementedException();
- }
- }
Now replace the OnActionExecuting() with,
- public override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- string message = Environment.NewLine + filterContext.ActionDescriptor.ControllerDescriptor.ControllerName +
- " & " + filterContext.ActionDescriptor.ActionName +
- @" = OnActionExecuting " + DateTime.Now.ToString("F") + Environment.NewLine;
- }
The code is already self explanatory, these filterContext statements are showing the name of controller and action.
Here we’ve another approach as well to show the controller names and actions with the help of magic strings.


vikram GPosted Mar 30, 2020, 5:52 AM
How to pass paramater to [LogTimeException]