ASP.NET Core & Web API (50 Qs)•Part 1: Request Pipeline, Dependency Injection & Web APIs (1 - 10)
What are Minimal APIs in .NET 8, and how do they compare to Controller-based APIs?
junior LevelMost AskedCode ExampleDependency InjectionPipesCI/CD & DevOps
In Simple Words:
- Controllers: Class-based, uses reflection and attribute routing (
[ApiController],[HttpGet]), heavier startup overhead and boilerplate. - Minimal APIs (introduced .NET 6, optimized .NET 8): Lambda-based route endpoints registered directly on
WebApplication. Uses source generators to eliminate reflection overhead at runtime, resulting in higher throughput and faster startup time.
csharp
// Minimal API Route in Program.cs:
app.MapPost("/api/orders", async (CreateOrderRequest request, IOrderService service, CancellationToken ct) =>
{
var order = await service.CreateOrderAsync(request, ct);
return Results.Created($"/api/orders/{order.Id}", order);
})
.RequireAuthorization("AdminOnly")
.WithName("CreateOrder");