liveBook · Manning

Minimal API

As explained in Building Web APIs with ASP.NET Core

Minimal APIs are an ASP.NET Core style for defining HTTP endpoints with minimal ceremony and without requiring controller classes.

Minimal APIs define HTTP endpoints with less ceremony than controller-based APIs. Route mappings and handlers can live in Program.cs, avoiding a separate controller class and often producing a smaller, more readable codebase. section 1.2.4 section 1.3

Use the approach when its simplicity matches the feature:

  • Map each route with methods such as MapGet and MapDelete, supplying a path and handler. section 1.2.4
  • Keep one-line implementations in Program.cs, but move substantial business logic into other classes. section 1.2.4
  • Prefer minimal APIs for simple web APIs and microservices; retain controllers when complex data operations make their structure more convenient. section 1.3 section 2.4.5

Mapping a board-game endpoint

A small endpoint can return board-game data directly: app.MapGet("/BoardGames", () => new[] { new BoardGame { Id = 1, Name = "Axis & Allies", Year = 1981 }, new BoardGame { Id = 2, Name = "Citadels", Year = 2000 }, new BoardGame { Id = 3, Name = "Terraforming Mars", Year = 2016 } }); The same behavior could be supplied by a controller, but the minimal form keeps the route and response together. section 2.4.5

// app.MapControllers();                                  #1
app.MapGet("/api/Sample",                                 #2
    () => "TODO: return all items");                      #2
app.MapGet("/api/Sample/{id}",                            #2
    (int id) => $"TODO: return the item with id #{id}");  #2
app.MapDelete("/api/Sample/{id}",                         #2
    (int id) => $"TODO: delete the item with id #{id}");  #2

Inspect the route mappings for collection, item, and delete endpoints, including a route parameter bound to an integer handler argument. section 1.2.4

Minimal API endpoints do not automatically receive controller-based Swagger grouping. Add explicit tags when related endpoints should appear under a named group such as Auth. section 11.2.7

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Shows route mapping, handler placement, controller trade-offs, and Swagger grouping without explaining framework internals.
Examples
Uses sample route mappings, board-game responses, an authorized test endpoint, and explicit Auth Swagger tags.
What you can do after
Apply it via ASP.NET Core route mapping, then choose between minimal APIs and controllers as endpoint complexity grows.
How it connects
Connects minimal APIs with controllers, Program.cs, microservices, business logic, authentication, authorization, and Swagger.

Also explained in

Compare how 3 books explain minimal API →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.