Introduction
Minimal APIs map routes to lambdas or methods in Program.cs — ideal for ShopNest microservices (inventory snapshot, health probes) without full controller ceremony.
After this article you will
- Map GET/POST/PUT/DELETE with MapGroup
- Bind route, query, and body parameters
- Inject services into route handlers
- Add filters and OpenAPI to minimal endpoints
- Compare minimal vs controller-based APIs
Prerequisites
- Article 41 — HTTP Client and External APIs
- ShopNest with EF Core and auth from Modules 2–4
Concept deep-dive
var inventory = app.MapGroup("/api/inventory").WithTags("Inventory");
inventory.MapGet("/", async (IInventoryService svc) =>
Results.Ok(await svc.GetAllAsync()));
inventory.MapGet("/{sku}", async (string sku, IInventoryService svc) =>
{
var item = await svc.GetBySkuAsync(sku);
return item is null ? Results.NotFound() : Results.Ok(item);
});
inventory.MapPost("/", async (CreateInventoryDto dto, IInventoryService svc) =>
{
var created = await svc.CreateAsync(dto);
return Results.Created($"/api/inventory/{created.Sku}", created);
}).RequireAuthorization();
| Minimal APIs | Controllers |
|---|---|
| Fast to prototype, less boilerplate | Better for large teams, filters, conventions |
| Great for microservices | Great for full ShopNest monolith MVC+API |
Hands-on — ShopNest Lightweight Microservice
- ShopNest.InventoryService — minimal API only project.
- MapGroup with route prefix and OpenAPI tag.
- Validation filter or Validate
on POST. - Dockerfile for lightweight container deploy.
Common errors & best practices
- Giant Program.cs — use extension methods MapInventoryEndpoints().
- Skipping authorization on write endpoints.
Interview questions
Q: When Minimal APIs?
A: Small services, BFFs, internal tools — not mandatory for all ShopNest APIs.
Q: DI in minimal handlers?
A: Parameters resolved from DI like controller constructors.
Summary
- Minimal APIs reduce ceremony for small services
- MapGroup organizes related endpoints
- Same auth, validation, OpenAPI as controllers
- ShopNest inventory microservice is ideal use case
Previous: HTTP Client and External APIs
Next: SignalR — Real-Time Web Applications
FAQ
Minimal API filters?
IEndpointFilter in .NET 7+ — like action filters for endpoints.
Test minimal APIs?
WebApplicationFactory integration tests hit endpoints in-memory.