Endpoint filters (minimal APIs)
“How do you add filters to a minimal API endpoint?”
The minimal-API version of an action filter: code that runs around one endpoint's handler and sees its arguments.
See it
POST newDay: 30
1/5The portal asks to move contract 5's payment day to the 30th. The request matches the endpoint.
In plain words
A minimal API is an endpoint written straight in Program.cs, with no controller class: app.MapPost("/payment-date", (ChangePaymentDateRequest req) => ...). The part after the path is a short inline function called the handler: it runs when a request matches. MVC is ASP.NET Core's controller-based style, and its filters (code that runs before or after a controller action, see ) don't run here, because there is no controller and no MVC. Instead, since .NET 7, you add an endpoint filter: a class that implements , attached to one endpoint or a group of endpoints with .AddEndpointFilter<T>(). Its one method gets the handler's arguments, already built from the request (not the raw request), and a next function that runs the rest. Code before await next(context) runs before the handler; code after it runs after. To stop the request, which is called short-circuiting, return a result yourself (for example a 400) and don't call next. The most common use is validation, so every endpoint doesn't repeat it.
- Minimal APIs don't run MVC filters; they have endpoint filters, added with
AddEndpointFilteron an endpoint or on a route group (a set of endpoints that share a path prefix), since .NET 7. - An gets an
EndpointFilterInvocationContextwith the bound (filled in) arguments and the , plusnext. It can run code before and after the handler, change the result, or short-circuit by returning a result without callingnext. - Several filters on one endpoint run in the order they're added on the way in, and in reverse on the way out, like middleware.
- I use them for validation and other per-endpoint checks. Things every request needs, like HTTPS, CORS and authentication, stay in middleware.
In the lending API
A validation filter for one endpoint, run on .NET 10. Program.cs is the whole path in four lines; the filter file is the detail, so the walk goes back to the start there. Tap Next line.
Lending.sln · where these 2 files sit
- src/Api/
- Filters/
- Filters/
What they ask next
Interviewers check that you can explain every line. Tap a question to open it.
How do you add the same filter to many endpoints?
Put them in a route group and add the filter once:
var payments = app.MapGroup("/payments").AddEndpointFilter<AuditFilter>();. Every endpoint mapped onpaymentsgets it.Endpoint filter or middleware?
Middleware runs for every request and can't see the handler's arguments. An endpoint filter runs for the endpoints you attach it to and gets the arguments already bound. Validation of a body: filter. HTTPS, CORS, authentication: middleware.
Traps
- Don't say
SayThat registers MVC filters, which only run for controller actions. Minimal APIs need endpoint filters.options.Filters.Add<T>()covers minimal APIs too.