Roadmap › ASP.NET Core Web API & REST › Module: REST Fundamentals
Lessons in this course (15)

4. Minimal APIs

Basic★ Must knowLesson 4 of 15
csharp
var orders = app.MapGroup("/api/orders").WithTags("Orders").RequireAuthorization();

orders.MapGet("/", async (IOrderService svc, [AsParameters] OrderQuery q, CancellationToken ct) =>
    TypedResults.Ok(await svc.ListAsync(q, ct)));

orders.MapGet("/{id:int}", async Task<Results<Ok<OrderDto>, NotFound>> (int id, IOrderService svc, CancellationToken ct) =>
    await svc.GetAsync(id, ct) is { } dto ? TypedResults.Ok(dto) : TypedResults.NotFound());

orders.MapPost("/", async (CreateOrderRequest req, IOrderService svc, CancellationToken ct) =>
{
    var dto = await svc.CreateAsync(req, ct);
    return TypedResults.Created($"/api/orders/{dto.Id}", dto);
}).AddEndpointFilter<ValidationFilter<CreateOrderRequest>>();
ControllersMinimal APIs
StyleClasses + attributesLambdas/handlers in code
FiltersMVC filtersEndpoint filters
Model validationAutomatic with [ApiController].NET 10 AddValidation() or FluentValidation/filters
Native AOTNot supportedSupported
Best forLarge APIs, teams used to MVCMicroservices, small/fast APIs