What are Minimal APIs in ASP.NET Core, and when would you choose them over traditional MVC controllers?
Short answer
Minimal APIs (introduced in .NET 6) let you define HTTP endpoints directly in Program.cs using simple lambda-based route handlers, without the boilerplate of a full controller class -- they fully support dependency injection, middleware, model binding, and authentication despite…
Advertisement
AModel answer
Minimal APIs (introduced in .NET 6) let you define HTTP endpoints directly in Program.cs using simple lambda-based route handlers, without the boilerplate of a full controller class -- they fully support dependency injection, middleware, model binding, and authentication despite their simplicity, and are fully compatible with Native AOT compilation. Minimal APIs suit lightweight services, microservices, or simple CRUD APIs prioritizing fast startup and minimal ceremony, while traditional controllers remain preferable for larger applications needing more structure, action filters, or a clear separation of many related endpoints.
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/products", () => new[] { "Product1", "Product2" });
app.MapGet("/products/{id}", (int id, IProductService svc) => svc.GetById(id)); // DI still works
app.Run();Real-world example
A small internal microservice exposing just three simple endpoints (list, get-by-id, create) uses Minimal APIs to avoid the overhead of a full controller class per endpoint, keeping the entire service's routing visible in a single, compact Program.cs file.
Likely follow-ups
- How do Minimal APIs handle model validation compared to the [ApiController] attribute's automatic validation in MVC?
- At what point does a growing Minimal API codebase typically benefit from being restructured into controllers instead?
Related topics