LinkGenerator creates paths and complete URLs from endpoint route information, avoiding hardcoded links that break when route templates change. It can be used in minimal API handlers, middleware, background services, and other application services. section 6.4 section 14.5.3
Generate a path from a named endpoint
Name the target endpoint with WithName("product"), inject LinkGenerator into another handler, and call GetPathByName("product", new { name = "big-widget" }). The generated path follows the endpoint's route template, such as /product/big-widget. section 6.4.1
Use the named-endpoint methods according to the URL you need:
- Call GetPathByName with route values to generate a path. section 6.4.1
- Call GetUriByName with a scheme and HostString to generate a complete URL; validate host values before using them. section 6.4.1
- Use GetPathByPage or GetPathByAction for Razor Pages and MVC actions. section 6.4 section 14.5.3
Endpoint names are case-sensitive and globally unique. Configure RouteOptions to standardize generated URLs with lowercase paths, lowercase query strings, or trailing slashes; pass LinkOptions when one URL needs different conventions. section 6.4.1 section 6.4.3
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.
Compare incoming routing, which turns a URL into route values, with URL generation, which combines route values and a template into a URL. section 6.4
