liveBook · Manning

Url generation

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

URL generation is the process of constructing endpoint URLs from routing metadata and route parameter values.

URL generation constructs endpoint URLs from route templates and route values. It keeps links and redirects independent of hardcoded paths, so changing an endpoint URL does not require updating every caller. section 6.0 section 6.4

Routing matches a URL to an endpoint and extracts route values; URL generation reverses that process by combining route values with a route template. ASP.NET Core provides LinkGenerator for minimal APIs and other application services. section 6.4

Use the appropriate helper for the application style:

  • Use LinkGenerator when generating URLs for minimal APIs or from middleware and other services. section 6.4
  • Use IUrlHelper in Razor Pages and MVC controllers; its Page() method accepts a page path and additional route data. section 14.5 section 14.5.1
  • Pass values matching route parameters to place them in the path; unmatched values become query-string data. section 14.5.1

Generating a Razor Page URL

A Razor Page can generate a currency-view link with Url.Page("Currency/View", new { code = "USD" }). The helper uses the page route template and the supplied code value. section 14.5.1

Figure 6.8 A comparison between routing and URL generation. Routing takes in a URL and generates route values, but URL generation uses route values to generate a URL.
CH06_F08_Lock3

Compare routing as URL-to-route-value mapping with URL generation as route-value-to-URL construction. section 6.4

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

How deep it goes
Explains reverse routing, route values, LinkGenerator, IUrlHelper, and query-string fallback, but not generator internals.
Examples
Uses renamed cart and basket paths, product/view/3, and a Currency/View page with the USD route value.
What it assumes
Assumes route templates and endpoint routing, while demonstrating URL generation for minimal APIs, Razor Pages, and MVC.
What you can do after
Apply it via LinkGenerator or IUrlHelper to create links and response redirects that follow route changes.
How it connects
Connects routing, route templates, minimal APIs, Razor Pages, MVC controllers, middleware, and redirects.

Also explained in

Compare how 2 books explain url generation →

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.