Understanding Action Filters in ASP.NET MVC

Introduction

When building web applications, developers often need to perform some tasks before or after an action method executes. Examples include:

  • Logging user activity

  • Checking authentication

  • Handling errors

  • Validating requests

Instead of writing the same code in every controller action, ASP.NET MVC provides a powerful feature called Action Filters.

Action Filters allow developers to run custom code before or after controller actions execute.

In this article, we will learn:

  • What Action Filters are

  • Why they are useful

  • Types of filters in ASP.NET MVC

  • A simple example with code

What Are Action Filters?

Action Filters are attributes that can be applied to controllers or action methods.

They allow developers to execute code:

  • Before an action method runs

  • After an action method runs

Example scenario:

When a user opens a page, we want to log the request time.

Instead of writing logging code inside every method, we create a filter.

Why Use Action Filters?

Action Filters help developers write clean and reusable code.

Code Reusability

The same filter can be applied to many controllers.

Separation of Concerns

Business logic stays inside controllers while cross-cutting tasks stay in filters.

Better Application Structure

Filters help keep code organized and maintainable.

Types of Filters in ASP.NET MVC

ASP.NET MVC provides several built-in filters.

Filter TypePurpose
Authorization FilterChecks user permissions
Action FilterRuns before and after action methods
Result FilterRuns before and after view rendering
Exception FilterHandles application errors

These filters help control how requests are processed.

Creating a Custom Action Filter

Now let's create a simple custom action filter.

Step 1: Create Filter Class

Create a new class called LogActionFilter.

using System;
using System.Web.Mvc;

public class LogActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        Console.WriteLine("Action method is starting at: " + DateTime.Now);
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        Console.WriteLine("Action method finished at: " + DateTime.Now);
    }
}

Explanation

Two important methods are used:

OnActionExecuting()

Runs before the action method starts.

OnActionExecuted()

Runs after the action method finishes.

Step 2: Apply Filter to Controller

Now apply the filter to a controller.

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

Explanation

When the Index action method runs, the filter will automatically execute.

Step 3: Apply Filter to Specific Action

Filters can also be applied to specific actions.

public class HomeController : Controller
{
    [LogActionFilter]
    public ActionResult About()
    {
        return View();
    }
}

Now the filter will run only for the About action method.

Example Request Flow

Suppose the user opens:

https://localhost:5000/Home/Index

The following steps occur:

  1. Filter executes OnActionExecuting()

  2. Controller action method runs

  3. Filter executes OnActionExecuted()

  4. View is returned to the browser

Real-World Example

Imagine a company application where every request needs to be logged.

Instead of writing logging code in 100 controllers, developers can create one Action Filter and apply it globally.

This makes the application:

  • Easier to maintain

  • Cleaner to read

  • More organized

Applying Filters Globally

Filters can also be applied to the entire application.

Open FilterConfig.cs.

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new LogActionFilter());
}

Now the filter runs for every request in the application.

Output

When a user opens a page, the log might show:

Action method is starting at: 10:15 AM
Action method finished at: 10:15 AM

This confirms the filter executed successfully.

Common Use Cases for Action Filters

Developers commonly use filters for:

  • Logging user activity

  • Checking authentication

  • Measuring performance

  • Error handling

  • Request validation

These tasks apply to many actions, so filters make development easier.

Conclusion

Action Filters are an important feature of ASP.NET MVC that help developers execute code before and after controller actions.

They allow developers to keep their applications clean, reusable, and well-structured.

In this article, we learned:

  • What Action Filters are

  • Why they are useful

  • Types of filters in MVC

  • How to create and use a custom filter