liveBook · Manning

Route data

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

Route data is a collection of named values supplied to an endpoint route during URL generation.

Route data is a set of named values supplied when generating a URL for an endpoint. Instead of hardcoding a link, give LinkGenerator an endpoint name and the values its route template needs. It combines them to produce a path, so a change to the template need not leave a hardcoded link broken. section 6.4 section 6.4.1

Listing 6.2 Generating a URL LinkGenerator and a named endpoint
app.MapGet("/product/{name}", (string name) => $"The product is {name}")  #1
    .WithName("product");                                                 #2
 
app.MapGet("/links", (LinkGenerator links) =>                             #3
{
    string link = links.GetPathByName("product",                          #4
        new { name = "big-widget"});                                      #4
    return $"View the product at {link}";                                 #5
});

The named endpoint uses the template /product/{name}. GetPathByName supplies name through a C# anonymous object, producing /product/big-widget. section 6.4.1

The anonymous object can hold more than one value: each property supplies a named route value. When the selected template contains that parameter, the value goes into the generated path. Going the other direction, routing matches an incoming URL against a template and extracts route values that can be passed to an endpoint handler through model binding. section 6.4.1 section 6.3.1

Defaults and missing values

With a template containing {category}, {name=all}, and {id?}, /product/shoes gives category=shoes and name=all, but no id value. The longer /product/shoes/all gives the same category and name values. An optional parameter without a default belongs at the end of the template. section 6.3.2

Use GetPathByName for a path or GetUriByName for a complete URL. Endpoint names are case-sensitive and must be unique. If you supply a host when generating a complete URL, do not use an unvalidated host value. section 6.4.1

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

How deep it goes
Works through anonymous-object route data and named-endpoint path generation; defers Razor Pages and MVC URL-generation methods.
Examples
Demonstrates a named product endpoint generating /product/big-widget and a default parameter giving /product/shoes the value name=all.
What it assumes
Introduces route parameters and C# anonymous-object values while using minimal API handlers and automatically registered LinkGenerator.
What you can do after
Apply it via LinkGenerator.GetPathByName to generate a path for a named minimal API endpoint without hardcoding the link.
How it connects
Connects route parameters and URL generation to extracted route values and model binding.

Also explained in

Compare how 2 books explain route data →

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.