Reusable request validation without controllers β
IEndpointFilterlets you intercept, validate, and short-circuit Minimal API requests in one clean, testable class.
If this sample saved you time, consider joining our Patreon community. You'll get exclusive .NET tutorials, premium code samples, and early access to new content β all for the price of a coffee.
π Join CodingDroplets on Patreon
Prefer a one-time tip? Buy us a coffee β
- How to implement
IEndpointFilterfor reusable Minimal API validation - How to short-circuit a request and return early (e.g.,
400 Bad Request) before the handler runs - How to chain multiple filters on a single endpoint
- How to keep
Program.csclean by extracting cross-cutting concerns into filter classes - How to test endpoint filters with Swagger/OpenAPI
Incoming HTTP Request
β
βΌ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Minimal API Pipeline β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Filter 1: ProductValidationFilter β β
β β β Validate request body β β
β β β Invalid? Return 400 immediately β β
β β β Valid? Call next() β β
β ββββββββββββββββββββ¬ββββββββββββββββββββββββββββββ β
β β β
β ββββββββββββββββββββΌββββββββββββββββββββββββββββββ β
β β Filter 2: (optional additional filters) β β
β ββββββββββββββββββββ¬ββββββββββββββββββββββββββββββ β
β β β
β ββββββββββββββββββββΌββββββββββββββββββββββββββββββ β
β β Endpoint Handler (the actual business logic) β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
200 OK / 400 Bad Request
| Stage | Action | Result |
|---|---|---|
| Pre-handler | Filter inspects request | Valid β continue; Invalid β short-circuit |
| Handler | Business logic executes | Returns response |
| Post-handler | Filter can inspect/modify response | Optional enrichment |
dotnet-minimal-api-endpoint-filters/
βββ dotnet-minimal-api-endpoint-filters.sln
βββ DotnetMinimalApiEndpointFilters.Api/
βββ Filters/
β βββ ProductValidationFilter.cs # IEndpointFilter implementation
βββ Models/
β βββ Product.cs # Request model
βββ Program.cs # Endpoint registration + filter wiring
βββ Properties/
βββ launchSettings.json
- .NET 10 SDK
- Any IDE: Visual Studio 2022+, VS Code, or JetBrains Rider
# Clone the repo
git clone https://github.com/codingdroplets/dotnet-minimal-api-endpoint-filters.git
cd dotnet-minimal-api-endpoint-filters
# Build and run
dotnet restore
dotnet run --project DotnetMinimalApiEndpointFilters.Api
# Open Swagger UI β http://localhost:{port}/swaggerpublic class ProductValidationFilter : IEndpointFilter
{
public async ValueTask<object?> InvokeAsync(
EndpointFilterInvocationContext context,
EndpointFilterDelegate next)
{
// Extract the request argument
var product = context.GetArgument<Product>(0);
// Validate β short-circuit if invalid
if (string.IsNullOrWhiteSpace(product.Name))
return Results.BadRequest("Product name is required.");
if (product.Price <= 0)
return Results.BadRequest("Price must be greater than zero.");
// Valid β pass through to the handler
return await next(context);
}
}app.MapPost("/api/products", (Product product) =>
{
// Only reached if the filter passes validation
return Results.Created($"/api/products/{product.Id}", product);
})
.AddEndpointFilter<ProductValidationFilter>();app.MapPost("/api/products", Handler)
.AddEndpointFilter<ProductValidationFilter>() // runs first
.AddEndpointFilter<LoggingFilter>(); // runs secondIEndpointFilter |
Action Filter | Middleware | |
|---|---|---|---|
| Scope | Single endpoint or group | Controller/action | Entire pipeline |
| Works with Minimal APIs | β | β | β |
| Works with Controllers | β | β | β |
| Access to endpoint metadata | β | β | β |
| Short-circuit support | β | β | β |
| Best for | Minimal API validation | MVC cross-cutting | Global concerns |
This project is licensed under the MIT License.
| Platform | Link |
|---|---|
| π Website | https://codingdroplets.com/ |
| πΊ YouTube | https://www.youtube.com/@CodingDroplets |
| π Patreon | https://www.patreon.com/CodingDroplets |
| β Buy Me a Coffee | https://buymeacoffee.com/codingdroplets |
| π» GitHub | http://github.com/codingdroplets/ |
Want more samples like this? Support us on Patreon or buy us a coffee β β every bit helps keep the content coming!