Routing selects the endpoint that handles an incoming HTTP request. Each endpoint has a route template describing the URLs it matches. This lets an application expose deliberate URLs, send multiple URLs to one handler, and capture values from a URL instead of writing a separate handler for every value. section 6.1 section 6.5
Capture a name from the URL
In a minimal API, `app.MapGet("/person/{name}", (string name) => ...)` registers a GET endpoint with a variable path segment. A request to `/person/Al` supplies `"Al"` as `name`; the handler can use it to find matching people and return them as JSON. section 5.2.1
Listing 5.1 A minimal API that uses a value from the URL
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
var people = new List<Person> #1
{ #1
new("Tom", "Hanks"), #1
new("Denzel", "Washington"), #1
new("Leondardo", "DiCaprio"), #1
new("Al", "Pacino"), #1
new("Morgan", "Freeman"), #1
}; #1
app.MapGet("/person/{name}", (string name) => #2
people.Where(p => p.FirstName.StartsWith(name))); #3
app.Run();
The complete minimal API shows the route template, its matching handler parameter, and the list the handler searches. section 5.2.1
Endpoint routing has two steps: RoutingMiddleware selects a registered endpoint, then EndpointMiddleware executes it. A URL that matches no route template leaves EndpointMiddleware without an endpoint to execute. Middleware between those steps can inspect the selected endpoint—for example, when applying authorization. WebApplication normally adds both routing components, so a minimal API usually does not add them manually. section 6.2 section 6.5 section 14.1
Route templates provide more than fixed paths:
- Parameters capture variable URL segments. An optional parameter must be the last segment; defaults can supply missing values. Use either sparingly when it would make an API harder to understand. section 6.5
- Constraints narrow which values match a route and can distinguish similar routes. They are not a substitute for validation. A catch-all parameter can capture the remaining path, including slashes. section 6.5
- URL generation creates links and redirect destinations from routing information rather than scattered hardcoded paths, making route changes easier to manage. section 6.0 section 14.5
The same routing infrastructure serves minimal APIs, Razor Pages, and MVC controllers, but they define mappings differently. Minimal APIs associate an explicit template with each endpoint. Razor Pages generate explicit templates from file paths: `Pages/Products/View.cshtml` matches `/products/view`. MVC controllers commonly use a conventional pattern that selects a controller and action; explicit routes offer finer control but require more declarations. Think carefully about these choices: routes are part of an application's public surface. section 14.2 section 14.3 section 19.6 section 6.5





















