liveBook · Manning

Route parameter

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

A route parameter is a named variable embedded in a URL pattern whose value is extracted from a matching request path.

A route parameter is a named variable inside a URL pattern. When the request path matches, ASP.NET Core extracts the variable value and makes it available to the endpoint handler. section 5.2.1 section 6.3.1

Filtering people by name

The route "/person/{name}" matches "/person/Al" and binds name to "Al". The handler can then filter people whose first names start with that value, returning matching objects as JSON. section 5.2.1

Use route parameters to define flexible URL patterns:

  • Write a parameter in braces, such as {category} or {name}; its URL segment may vary, but the segment is required unless marked optional. section 6.3.1
  • Combine literal segments and parameters, such as /{category}/{name}, to match product paths like /bags/rucksack-a. section 6.3.1
  • Add ? to make a final parameter optional, or use =value to supply a default when the URL omits it. section 6.3.2
  • Treat optional and default parameters sparingly because they can make APIs harder to understand; use constraints to restrict matching, not to perform validation. section 6.5
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();

Inspect a complete minimal API that declares a {name} parameter, binds it to a string, and uses it in endpoint logic. section 5.2.1

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

How deep it goes
Defines required, optional, and default parameters with examples, while deferring broader routing mechanics and syntax.
Examples
Uses a people-search API, product-category URLs, optional product identifiers, and Razor Page product paths.
What it assumes
Assumes basic minimal API endpoint definitions and introduces route-value extraction before broader routing features.
What you can do after
Build it from scratch by declaring named URL variables and consuming their bound values in minimal API handlers.
How it connects
Connects route templates with URL routing, minimal APIs, model binding, constraints, Razor Pages, and MVC.

Also explained in

Compare how 6 books explain route parameter →

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.