Action filter
Action filter will be called before and after the action starts executing and after the action has executed. We can put our custom pre-processing and post-processing logic in this filter.
Now to implement this filter we need to create a custom filter attribute class and implement the
IActionFilter interface
This interface provides us two methods OnActionExecuting and OnActionExecuted which will be called before and after the action gets executed respectively.
public class CustomActionAttribute : FilterAttribute, IActionFilter
{
void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.Controller.ViewBag.OnActionExecuted = "IActionFilter.OnActionExecuted filter called";
}
void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.Controller.ViewBag.OnActionExecuting = "IActionFilter.OnActionExecuting filter called";
}
}
Authorization filter
This filter provides authentication and authorization logic. It will be executed before the action gets executed. To implement this action the interface IAuthorizationFilter should be implemented by the custom attribute class.
public class CustomAuthorizationAttribute : FilterAttribute, IAuthorizationFilter
{
void IAuthorizationFilter.OnAuthorization(AuthorizationContext filterContext)
{
filterContext.Controller.ViewBag.OnAuthorization = "IAuthorizationFilter.OnAuthorization filter called";
}
}
Result filter
Result filter will execute before and after the result of the action method has been executed. We can use this filter if we want some modification to be done in the action's result.
IResultFilter interface
This interface provides two methods OnResultExecuting and OnResultExecuted which will be called before and after the action result respectively.
public class CustomResultAttribute : FilterAttribute, IResultFilter
{
void IResultFilter.OnResultExecuted(ResultExecutedContext filterContext)
{
filterContext.Controller.ViewBag.OnResultExecuted = "IResultFilter.OnResultExecuted filter called";
}
void IResultFilter.OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.Controller.ViewBag.OnResultExecuting = "IResultFilter.OnResultExecuting filter called";
}
}
Exception filter
Exception filter will be invoked whenever a controller or action of the controller throws an exception. This is particularly useful when we need custom error logging module.
To implement this filter we need to create a custom filter attribute class which implements IExceptionFilter. This interface gives us a methods called OnException which is a perfect place to call the exception logging module and to redirect to some error page.
public class CustomExceptionAttribute : FilterAttribute, IExceptionFilter
{
void IExceptionFilter.OnException(ExceptionContext filterContext)
{
filterContext.Controller.ViewBag.OnException = "IExceptionFilter.OnException filter called";
}
}
Order of Execution of action filter as following
IAuthorizationFilter.OnAuthorization
IActionFilter.OnActionExecuted
IActionFilter.OnActionExecuting
IResultFilter.OnResultExecuted
IResultFilter.OnResultExecuting
IExceptionFilter.OnException