liveBook · Manning

Chapter 6: Mapping URLs to endpoints using routing

What you will learn in chapter 6 of ASP.NET Core in Action, 3e

This chapter introduces routing as the system that selects endpoint handlers from request URLs, matches flexible route templates, and generates URLs without hardcoding them.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces routing as the system that selects endpoint handlers from request URLs, matches flexible route templates, and generates URLs without hardcoding them.

Key terms: routing, endpoint routing, route template, minimal APIs, Razor Pages, MVC, endpoint, endpoint handler, incoming request URL, middleware pipeline

6.1 What is routing?

This section introduces routing as the process that selects a request handler and explains why customizable, dynamic URLs are preferable to purely static mappings or query-string-based URLs.

Key terms: routing, EndpointMiddleware, handler, endpoint handler, page handler method, action method, static URL-based mapping, query string, model binding, dynamic URLs

6.2 Endpoint routing in ASP.NET Core

This section explains how endpoint routing registers URL-patterned handlers and uses two middleware stages to select and then execute an endpoint in the ASP.NET Core pipeline.

Key terms: endpoint, route template, IEndpointRouteBuilder, WebApplication, MapGet, MapHealthChecks, EndpointRoutingMiddleware, RoutingMiddleware, EndpointMiddleware, HttpContext

6.3 Exploring the route template syntax

Introduces the route-template features that the following subsections explore beyond fixed paths and basic parameters.

Key terms: route template, fixed values, route parameter, default values, optional segments, constraints

6.3.1 Working with parameters and literal segments

This section explains how route templates use slash-separated literal segments and required parameters to match URLs and capture route values.

Key terms: route template, segment, literal segment, route parameter, complex segment, route value, route values, model binding, route-value dictionary

6.3.2 Using optional and default values

This section explains how default values and optional parameters in route templates let one endpoint match multiple URL shapes.

Key terms: default value, route parameter, route value, route template, optional route parameter, {id?}, optional parameter, default values, optional parameters

6.3.3 Adding additional constraints to route parameters

This section explains how route constraints narrow route-template matches, prevent incompatible route values, and help—but do not fully solve—ambiguous overlapping routes.

Key terms: route constraint, route template, route parameter, IntRouteConstraint, {id:int}, route constraints, invariant culture, regular expression, overlapping routes, 404

6.3.4 Matching arbitrary URLs with the catch-all parameter

This section explains how catch-all route parameters capture an arbitrary remainder of a URL and how their one- and two-asterisk forms differ during URL generation.

Key terms: catch-all parameter, route template, route value, URL segments, {*others}, {**others}, greedy, overlap, URL encoding, round-trip behavior

6.4 Generating URLs from route parameters

This section introduces URL generation as routing in reverse, using route values and route templates to create maintainable links and redirects dynamically.

Key terms: URL generation, routing infrastructure, route templates, routing, route values, route template, routing middleware, LinkGenerator, minimal APIs, named routes

6.4.1 Generating URLs for a minimal API endpoint with LinkGenerator

This section shows how minimal API endpoints use the DI-provided LinkGenerator and endpoint names to generate paths or full URLs from route values.

Key terms: WithName(), endpoint metadata, endpoint names, minimal API endpoint, LinkGenerator, dependency injection container, handler, GetPathByName(), GetUriByName(), route data

6.4.2 Generating URLs with IResults

This section explains how minimal API IResults generate redirect responses to named endpoints or arbitrary URLs and how redirect status codes are selected.

Key terms: Results.RedirectToRoute(), named endpoint, route parameters, IResult, 302 Found, Location header, permanent, preserveMethod, 301 Moved Permanently, 307 Temporary Redirect

6.4.3 Controlling your generated URLs with RouteOptions

This section shows how to set global URL-generation conventions with RouteOptions and override them for individual LinkGenerator calls with LinkOptions.

Key terms: routing, LinkGenerator, route template, case-insensitive comparison, RouteOptions, Configure<T>, WebApplicationBuilder.Services, LowercaseUrls, LowercaseQueryStrings, AppendTrailingSlash

6.5 Summary

This summary recaps how ASP.NET Core routing matches request URLs to endpoints and generates maintainable URLs from endpoint routes.

Where this is used later

Concepts explained in this chapter

← Chapter 5: Creating a JSON API with minimal APIs Chapter 7: Model binding and validation in minimal APIs →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.