Lesson 11 / 26

Your First Minimal API

Create a web app with one endpoint and understand the builder and app objects.

Builder then app

WebApplication.CreateBuilder collects configuration and services. builder.Build() makes the app, where you map endpoints and add middleware. app.Run() starts the web server (Kestrel).

Life of an HTTP request

A request passes through middleware, is matched to an endpoint, runs your handler, and the response travels back.

Five stages from incoming request to outgoing response.
Figure 4.1 — The ASP.NET Core request pipeline.

Program.cs

Create it with dotnet new web. This maps one GET endpoint at / that returns text.

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello from .NET");

app.Run();

Use launchSettings for the port

During development the port comes from Properties/launchSettings.json. In production set it with the ASPNETCORE_URLS environment variable rather than hard-coding it.

Quick check: Which call maps a GET endpoint in a minimal API?

  • app.UseGet
  • app.MapGet
  • builder.AddGet
  • app.RunGet
Answer

app.MapGet — `MapGet(pattern, handler)` registers a handler for GET requests on a route.