← Foundations path
.NETFoundations 32 / 37Asked by interviewers

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

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.

Say it in the interview

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/
Diagram · step 1The portal asks to move contract 5's payment day to the 30th. The request matches the endpoint.
Program.cs · line 1 · 1 of 4Maps POST requests for this path to the handler below.

What they ask next

Interviewers check that you can explain every line. Tap a question to open it.

Traps

Read more: Filters in minimal API apps