Controller filters in ASP.NET Core Web APIs wrap defined stages of controller execution. They fit cross-cutting behavior that needs action arguments, model state, controller metadata, or the action result. A correlation-ID rule tied only to selected controllers is one example. An application-wide correlation ID usually belongs in middleware.
The filter APIs live in Microsoft.AspNetCore.Mvc, but they also apply to Web API controllers derived from ControllerBase; rendering views is not required. Minimal API handlers use endpoint filters, which have a different invocation context. Endpoint filters can also be explicitly attached to mapped controller actions.
The boundary matters. Middleware can cover every request type but lacks controller action context. Endpoint code has full local context but duplicates a rule when many actions need it. Filters occupy the space between those two.
Controller filters run after routing has selected a controller action.
- Authorization filters run first and can short-circuit unauthorized requests.
- Resource filters run around most of the rest of the pipeline and can short-circuit early.
- Action filters run before and after the action method.
- Exception filters observe unhandled exceptions from action execution.
- Result filters run before and after the action result is executed.
Scope and filter type both affect nesting. IOrderedFilter.Order can override the default scope order, so a custom order should be treated as part of the endpoint’s behavior.
Example
This async action filter rejects selected requests without a correlation header:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
public sealed class RequireCorrelationIdFilter : IAsyncActionFilter
{
public async Task OnActionExecutionAsync(
ActionExecutingContext context,
ActionExecutionDelegate next)
{
if (!context.HttpContext.Request.Headers.ContainsKey("X-Correlation-Id"))
{
context.Result = new BadRequestObjectResult(new
{
error = "X-Correlation-Id header is required"
});
return;
}
await next();
}
}
// Program.cs
builder.Services.AddScoped<RequireCorrelationIdFilter>();
builder.Services.AddControllers(options =>
{
// Global registration
options.Filters.AddService<RequireCorrelationIdFilter>();
});For one action or controller, [ServiceFilter(typeof(RequireCorrelationIdFilter))] applies the registered filter without making it global.
An exception filter can translate an unhandled exception from action execution into an API error result:
public sealed class ApiExceptionFilter(ILogger<ApiExceptionFilter> logger) : IAsyncExceptionFilter
{
public Task OnExceptionAsync(ExceptionContext context)
{
logger.LogError(context.Exception, "Unhandled exception in {Action}",
context.ActionDescriptor.DisplayName);
context.Result = context.Exception is NotFoundException
? new NotFoundObjectResult(new { error = context.Exception.Message })
: new ObjectResult(new { error = "An unexpected error occurred." }) { StatusCode = 500 };
context.ExceptionHandled = true;
return Task.CompletedTask;
}
}Global registration applies it to every controller action: builder.Services.AddControllers(opts => opts.Filters.Add<ApiExceptionFilter>());. Middleware remains the broader choice when errors from routing or Minimal API handlers need the same response contract.
Applying Filters: [ServiceFilter] Vs [TypeFilter] Vs IFilterFactory
Attributes cannot receive runtime services through their own constructors, so attachment determines how the real filter instance is created.
[ServiceFilter(typeof(MyFilter))]resolves the filter itself from DI, soMyFiltermust be registered with the intended lifetime.[TypeFilter(typeof(MyFilter))]usesActivatorUtilities. Constructor services come from DI, whileArgumentscan supply literal values. The filter type itself does not need registration.IFilterFactorygives an attribute explicit control over creation of the executable filter and whether the result can be reused.- A plain filter attribute can carry literal metadata but cannot accept runtime services in its attribute constructor.
[Authorize] contributes authorization metadata. Under default endpoint routing, authorization middleware enforces that metadata before the controller endpoint runs. Duplicating policy checks in an action filter creates two access-control paths. Within a filter type, the usual nesting is global, controller, then action on the way in and the reverse on the way out. If a filter implements both sync and async forms of the same stage, the runtime invokes only the async interface. The filter’s synchronous methods then run only when that async implementation calls them, as the default async methods of ActionFilterAttribute do, so custom filter logic belongs in one form per stage.
Pitfalls
- Blocking I/O:
.Resultor synchronous remote calls hold request threads while no CPU work is happening. I/O-bound filters should implement the async interface and await the operation. - Authorization in action filters: a second permission system drifts away from registered policies. Access decisions belong in authentication handlers and authorization policies.
- Treating exception filters as global error handling: exception filters cover unhandled exceptions from controller creation, model binding, action filters, and action methods. They do not cover middleware, routing, resource filters, result filters, or action-result execution. Exception-handling middleware is the safer outer boundary for a uniform API error contract.
Tradeoffs
| Option | Best for | Weakness |
|---|---|---|
| Middleware | App-wide cross-cutting concerns (logging, auth, exception handling) | No direct controller action context |
| Controller filters | Concerns tied to Web API controllers/actions and model/action context | Their stages do not apply to Minimal API handlers |
| Endpoint filters | Minimal API handler behavior, or explicit attachment to mapped controller actions | Invocation context differs from controller model-state and action-result context |
Questions
After routing selects a controller action, the filter stages run in this order: authorization, resource, action, then result. Exception filters are not a sequential stage; they run only when an unhandled exception occurs.
- Authorization filters run first and can short-circuit an unauthorized request.
- Resource filters run their before logic next, ahead of model binding, and their after logic last, once the rest of the pipeline has completed.
- Action filters run immediately before and after the action method.
- Result filters run immediately before and after the action result executes.
Full flow: authorization → resource (before) → action (before) → action method → action (after) → result (before) → result execution → result (after) → resource (after).
Action filters finish before result filters start, so an action filter’s after logic can still replace the result. Only resource filters wrap both the action and the result stages.
These are controller-filter stages. Minimal API handlers use endpoint filters rather than this staged controller pipeline.
An exception filter catches unhandled exceptions from running the action: the action method and the steps right around it, which are creating the controller, model binding and the action filters. It does not see anything that goes wrong before those steps or after them. An exception thrown while the result is written to the response, such as a serialization error, gets past it, and so does one thrown in middleware. That is why an exception filter suits error handling for controllers only, while one error response for the whole API belongs in exception-handling middleware.