Minimal APIs — lightweight alternative to MVC controllers.
Comparison:
Minimal API with filters:
1var api = app.MapGroup("/api")2 .AddEndpointFilter<ValidationFilter>()3 .RequireAuthorization();45api.MapPost("/orders", async (CreateOrderDto dto, IOrderService svc) =>6{7 var order = await svc.CreateAsync(dto);8 return Results.Created($"/orders/{order.Id}", order);9})10.WithName("CreateOrder")11.WithTags("Orders");
Endpoint filters (AOP):
1public class ValidationFilter : IEndpointFilter2{3 public async ValueTask<object?> InvokeAsync(4 EndpointFilterInvocationContext ctx,5 EndpointFilterDelegate next)6 {7 var validator = ctx.HttpContext.RequestServices8 .GetRequiredService<IValidator<T>>();9 var result = await validator.ValidateAsync(input);10 if (!result.IsValid)11 return Results.ValidationProblem(result.ToDictionary());12 return await next(ctx);13 }14}
When to use which: