aspnet
  1. aspnet-action-filters

Action Filters in ASP.NET MVC

Action Filters in ASP.NET MVC are attributes that you can apply to an action method or a controller. These attributes are used to execute a particular piece of code before or after the execution of an action method. Action Filters can also be used to cancel a request or modify the response.

Syntax

Action Filters in ASP.NET MVC are created by extending the System.Web.Mvc.FilterAttribute class and implementing either System.Web.Mvc.IActionFilter, System.Web.Mvc.IAuthorizationFilter, System.Web.Mvc.IExceptionFilter or System.Web.Mvc.IResultFilter interface depending on the type of operation to be performed.

using System.Web.Mvc;

public class CustomActionFilterAttribute: ActionFilterAttribute
{
   //override the method from the FilterAttribute class based on type of operation to be performed i.e. Action, Authorization,
   //Result and Exception filters
}

Example

Let's have a look at an example where you can use an action filter to log the execution time of an action method.

public class LogActionFilter : ActionFilterAttribute
{
    private Stopwatch stopwatch;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        stopwatch = new Stopwatch();
        stopwatch.Start();
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        stopwatch.Stop();
        var controller = filterContext.Controller.ToString();
        var action = filterContext.ActionDescriptor.ActionName;
        var executionTime = stopwatch.ElapsedMilliseconds.ToString();
        Debug.WriteLine($"{controller}.{action} took {executionTime}ms to execute");
    }
}

In the above example, LogActionFilter is an action filter that logs the execution time of an action method. We are inheriting our class from ActionFilterAttribute class and we are overriding the OnActionExecuting and OnActionExecuted method which will get executed before and after the action method gets executed respectively.

After running the above example, you should be able to see the execution time for each action method printed in the output window.

Output

Action filters are used to modify the behavior of the action method and its output. The output of an action filter can be used to change the response sent back to the client, redirect the client to a different URL, or write some logs.

Explanation

Action filters in ASP.NET MVC are attributes that allow you to inject pre-action and post-action behavior into your MVC controllers. When you apply action filters to a controller action method, they can override the method invocation, change the input parameters before the method is executed, or change the output parameters after the method is executed.

Use

Action Filters can be used to:

  • Perform authentication and authorization
  • Log requests and responses
  • Modify response data before it is sent to the client
  • Handle exceptions
  • Add caching
  • Restrict access to a method by applying policies

Important Points

  • Action filters can be applied to a controller action method or a controller.
  • An action filter is an attribute that you can decorate to any Action Method and/or Action Class.
  • Action filters execute in a specific order before or after an action method gets executed.
  • By implementing Action Filters we can achieve common functionality across multiple controllers.

Summary

In this page, you have learned about Action Filters in ASP.NET MVC. We discussed the syntax, examples, output, explanation, use, and important points of Action Filters. By using Action Filters, you can change the behavior of a controller method, alter input and output parameters, and inject additional behavior into your application.

Published on: