liveBook · Manning

Routing

As explained in ASP.NET Core in Action, 3e

Routing is the mechanism that maps incoming requests to application endpoints or handlers.

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

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains endpoint selection and execution, then develops route parameters, constraints, conventions, and URL generation.
Examples
Uses a `/person/{name}` minimal API, a file-path-derived Razor Page route, and a default MVC controller route.
What it assumes
Assumes familiarity with C# or a similar object-oriented language; treats familiarity with HTML and JavaScript as beneficial.
What you can do after
Build it from scratch with route templates, then generate links and choose between explicit and conventional mappings.
How it connects
Connects routing to the middleware pipeline, route templates, minimal APIs, Razor Pages, MVC controllers, authorization, and URL generation.

Also explained in

Compare how 22 books explain routing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.