askvity

What is Custom Action Filter?

Published in Custom Action Filter 3 mins read

A custom action filter is essentially an attribute that you create yourself and apply to a controller action—or an entire controller—to modify the way in which the action is executed, tailored to your specific application needs.

Understanding Action Filters

To grasp what a custom action filter is, let's first understand what an action filter is in general. Based on the provided reference, 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."

These filters are part of the ASP.NET Core MVC (or similar framework) pipeline. They allow you to inject logic that runs either before or after a controller action method executes, or even before or after the action result is processed. Frameworks often provide built-in action filters (like [Authorize] or [RequireHttps]).

What Makes it "Custom"?

The term "custom" simply means that you, the developer, have written the code for this filter. Instead of using a pre-defined filter provided by the framework, you implement interfaces or inherit from base classes provided by the framework to create an action filter with behaviour specific to your application's requirements.

Why Use Custom Action Filters?

Custom action filters are powerful because they promote the Don't Repeat Yourself (DRY) principle. Instead of writing the same cross-cutting concern logic in multiple action methods, you can encapsulate it in a filter and apply that filter where needed.

Common use cases for creating custom action filters include:

  • Logging: Log details about requests and responses before/after action execution.
  • Authorization/Authentication: Implement custom logic for checking user permissions beyond standard roles.
  • Input Validation: Perform complex model validation or data sanitation before the action executes.
  • Output Caching: Implement custom caching strategies for action results.
  • Modifying Results: Transform or wrap action results (e.g., standardizing API responses).
  • Error Handling: Catch specific exceptions and modify the response.

How They Work (Simplified)

Custom action filters typically implement interfaces like IActionFilter or IAsyncActionFilter. These interfaces define methods that the framework calls at specific points in the request pipeline:

  • OnActionExecuting / OnActionExecutionAsync: Runs before the target action method is called.
  • OnActionExecuted / OnActionExecutionAsync: Runs after the target action method has finished, but before the result is processed.

By implementing these methods, your custom filter can inspect or modify the ActionExecutingContext or ActionExecutedContext objects, giving you control over the execution flow, the request data, and the action's result.

Essentially, a custom action filter allows you to intercept and influence the execution of controller actions in a reusable and declarative way, using an attribute on your methods or classes.

Related Articles