Introduction
ASP.NET Core Minimal APIs have become a popular way to build lightweight and high-performance APIs. However, as applications grow, developers often need to perform common tasks before or after an endpoint executes.
Examples include:
Request validation
Logging
Authentication checks
Performance monitoring
Response modification
Before .NET 7, developers typically used Middleware or custom logic inside endpoints. To simplify these scenarios, Microsoft introduced Endpoint Filters.
Endpoint Filters provide a clean way to run code before and after a Minimal API endpoint executes, making applications easier to maintain and extend.
In this article, you'll learn what Endpoint Filters are, how they work, and how to implement them in ASP.NET Core applications.
What Are Endpoint Filters?
Endpoint Filters allow developers to intercept requests before and after a Minimal API endpoint executes.
Think of them as a pipeline around an endpoint.
Request
↓
Endpoint Filter
↓
Endpoint
↓
Response
This makes it possible to add reusable logic without cluttering endpoint code.
Why Use Endpoint Filters?
Consider a simple endpoint:
app.MapPost("/products",
(Product product) =>
{
return Results.Ok(product);
});
Suppose you need validation for multiple endpoints.
Without Endpoint Filters:
Endpoint 1
Validation Logic
Endpoint 2
Validation Logic
Endpoint 3
Validation Logic
The same code gets repeated.
With Endpoint Filters:
Filter
↓
Reusable Validation
↓
All Endpoints
This improves maintainability.
How Endpoint Filters Work
An Endpoint Filter implements the IEndpointFilter interface.
Example:
public class LoggingFilter
: IEndpointFilter
{
public async ValueTask<object?>
InvokeAsync(
EndpointFilterInvocationContext context,
EndpointFilterDelegate next)
{
Console.WriteLine(
"Request Started");
var result =
await next(context);
Console.WriteLine(
"Request Completed");
return result;
}
}
The filter executes code before and after the endpoint.
Registering an Endpoint Filter
Attach the filter to an endpoint.
app.MapGet("/users",
() => Results.Ok("Users"))
.AddEndpointFilter<LoggingFilter>();
When the endpoint executes:
Request Started
↓
Endpoint Executes
↓
Request Completed
The filter automatically runs.
Using Endpoint Filters for Validation
A common use case is request validation.
Example:

Join the conversation! Your thoughts help the community grow.