ASP.NET MVC Custom Filters

An Action filter is an attribute that you can apply to a controller action — or an entire controller — that modifies the way in which the action is executed.

ASP.NET MVC provides Action Filters for executing filtering logic either before or after an action method is called. Action Filters are custom attributes that provide declarative means to add pre-action and post-action behavior to the controller’s action methods.

The ASP.NET MVC framework supports four different types of filters:

  1. Authorization filters – Implements the IAuthorizationFilter attribute.
  2. Action filters – Implements the IActionfilter attribute.
  3. Result filters – Implements the IResultFilter attribute.
  4. Exception filters – Implements the IExceptionFilter attribute.

Authorization filters are used to implement authentication and authorization for controller actions, for example: Authorize filter is an example of an Authorization filter.

Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance to modify the view data that a controller action returns.

Result filters contain logic that is executed before and after a view result is executed. For example, you might want to modify a view result right before the view is rendered to the browser.

Exception filters are the last type of filter to run. You can use an exception filter to handle errors raised by either your controller actions or controller action results. You also can use exception filters to log errors.

The base ActionFilterAttribute class has the following methods that you can override:

  • OnActionExecuting – This method is called before a controller action is executed.
  • OnActionExecuted – This method is called after a controller action is executed.
  • OnResultExecuting – This method is called before a controller action result is executed.
  • OnResultExecuted – This method is called after a controller action result is executed.

Sample code for your reference:

public class MyLogActionFilter : ActionFilterAttribute

{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
LogException(“OnActionExecuting”, filterContext.RouteData);
}

public override void OnActionExecuted(ActionExecutedContext filterContext)
{
LogException(“OnActionExecuted”, filterContext.RouteData);
}

public override void OnResultExecuting(ResultExecutingContext filterContext)
{
LogException(“OnResultExecuting”, filterContext.RouteData);
}

public override void OnResultExecuted(ResultExecutedContext filterContext)
{
LogException(“OnResultExecuted”, filterContext.RouteData);
}

private void LogException(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, “Action Filter Log”);
}

Below sample code will illustrates how you can apply the log action filter to entire controller class :

Whenever any of the actions exposed by the Home controller are invoked – the processing of action are logged to Debug.WriteLine (i.e. visual studio output window).

[MyLogActionFilter]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}