ASP.NET Core › Controllers vs Minimal APIs

How do you implement equivalent cross-cutting concerns (like automatic model validation) in minimal APIs that [ApiController] provides automatically for controllers?

Advanced

Answer

Since minimal APIs lack [ApiController]'s automatic ModelState validation, you implement it via a custom IEndpointFilter that validates the incoming DTO (often using DataAnnotations' Validator class or a library like FluentValidation) before the endpoint handler runs, returning a 400 response with validation errors if invalid -- applied globally to a route group via .AddEndpointFilter() rather than needing per-action boilerplate.
public class ValidationFilter<T> : IEndpointFilter {
    public async ValueTask<object?> InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next) {
        var dto = context.GetArgument<T>(0);
        var validationResults = new List<ValidationResult>();
        if (!Validator.TryValidateObject(dto, new ValidationContext(dto), validationResults, true))
            return Results.ValidationProblem(validationResults.ToDictionary(...));
        return await next(context);
    }
}

app.MapPost("/products", Create).AddEndpointFilter<ValidationFilter<ProductDto>>();
Real-world example A team building a large minimal-API-based project writes one reusable generic ValidationFilter<T> applied to every endpoint accepting a DTO, replicating [ApiController]'s automatic validation behavior consistently without per-endpoint duplication.

Common follow-ups: How does this compare in complexity/maintainability to just using controllers with [ApiController]?;What other MVC conveniences commonly need to be reimplemented for minimal APIs?