Skip to the workspace
Pebellslearn by building
ASP.NET Core Basics: Endpoints  ›  Lesson

Minimal APIs — An Endpoint in One Line

Minimal APIs 15 minA Minimal API maps an HTTP method and route straight to a lambda whose parameters are bound from the request and whose return value becomes the response.

aspnet core / Minimal APIs — An Endpoint in One Line

Explore each scene at your own pace.

01 · The rule

The rule

Minimal APIs — An Endpoint in One Line

A new ASP.NET Core project does not make you create folders of classes before you can say hello. With Minimal APIs, ReadingRoom's first endpoint is one line in Program.cs. It is the default style of the webapi template, and it scales further than its name suggests.

The rule

Map a method and route to a function; parameters bind in, results go out.

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<BookStore>();      // register services
var app = builder.Build();

app.MapGet("/books", (BookStore store) => store.All());

app.MapGet("/books/{id:int}", (int id, BookStore store) =>
    store.Find(id) is Book book
        ? TypedResults.Ok(book)
        : Results.NotFound());

app.MapPost("/books", (Book book, BookStore store) =>
{
    store.Add(book);
    return TypedResults.Created($"/books/{book.Id}", book);
});

app.Run();

MapGet, MapPost, MapPut, MapPatch and MapDelete each pair a method with a route. {id:int} is a route parameter with a constraint: /books/abc never matches.

Binding fills each lambda parameter from the right place:

  • a name that matches a route segment → from the route (id)
  • a simple type not in the route → from the query string (?page=2)
  • a complex type on POST/PUT → from the JSON body (Book book)
  • a registered service → from dependency injection (BookStore store)

Attributes such as [FromQuery] or [FromHeader] override the guess. Whatever you return is written back: an object becomes JSON with 200 OK; Results/TypedResults let you choose the status. app.MapGroup("/books") shares a prefix across related endpoints.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.