Skip to content

codingdroplets/dotnet-minimal-api-endpoint-filters

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Minimal API Endpoint Filters in ASP.NET Core (.NET 10)

Reusable request validation without controllers β€” IEndpointFilter lets you intercept, validate, and short-circuit Minimal API requests in one clean, testable class.

.NET ASP.NET Core License: MIT Visit CodingDroplets YouTube Patreon Buy Me a Coffee GitHub


πŸš€ Support the Channel β€” Join on Patreon

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 β˜•


🎯 What You'll Learn

  • How to implement IEndpointFilter for 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.cs clean by extracting cross-cutting concerns into filter classes
  • How to test endpoint filters with Swagger/OpenAPI

πŸ—ΊοΈ Architecture Overview

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

πŸ“‹ Filter Execution Flow

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

πŸ“ Project Structure

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

πŸ› οΈ Prerequisites

  • .NET 10 SDK
  • Any IDE: Visual Studio 2022+, VS Code, or JetBrains Rider

⚑ Quick Start

# 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}/swagger

πŸ”§ How It Works

Step 1 β€” Implement IEndpointFilter

public 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);
    }
}

Step 2 β€” Wire the Filter to an Endpoint

app.MapPost("/api/products", (Product product) =>
{
    // Only reached if the filter passes validation
    return Results.Created($"/api/products/{product.Id}", product);
})
.AddEndpointFilter<ProductValidationFilter>();

Step 3 β€” Chain Multiple Filters (Optional)

app.MapPost("/api/products", Handler)
   .AddEndpointFilter<ProductValidationFilter>()   // runs first
   .AddEndpointFilter<LoggingFilter>();             // runs second

πŸ€” IEndpointFilter vs Action Filter vs Middleware

IEndpointFilter 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

πŸ“š References


πŸ“„ License

This project is licensed under the MIT License.


πŸ”— Connect with CodingDroplets

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!

About

ASP.NET Core Minimal API sample using endpoint filters for reusable request validation, with Swagger/OpenAPI and beginner-friendly structure.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages