Middleware affects all endpoints. Endpoint Filters apply logic only to specific endpoints you choose.
Create Filter:
public class LoggingFilter : IEndpointFilter
{
public async ValueTask
Apply to Specific Endpoint:
app.MapGet("/api/data", () => "Hello")
.AddEndpointFilter();
// Only this endpoint gets the filter!
Inline Filter:
app.MapPost("/api/users", (User user) => Results.Created())
.AddEndpointFilter(async (context, next) =>
{
// Validation
var user = context.GetArgument(0);
if (string.IsNullOrEmpty(user.Email))
return Results.BadRequest("Email required");
return await next(context);
});
Use Cases: Validation, logging, auth checks, rate limiting – per endpoint!
