liveBook · Manning

Chapter 14: Mapping URLs to Razor Pages using routing

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

This chapter introduces how ASP.NET Core routing selects Razor Pages, how their routes can be customized, and how URLs can be generated for them.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces how ASP.NET Core routing selects Razor Pages, how their routes can be customized, and how URLs can be generated for them.

Key terms: routing, Razor Pages, middleware pipeline, MVC framework, endpoint routing, default routing behavior, route templates, explicit route templates, conventions, Razor Pages conventions

14.1 Routing in ASP.NET Core

This section explains how ASP.NET Core endpoint routing registers Razor Page endpoints, selects a matching endpoint for each request, and then executes it.

Key terms: RoutingMiddleware, EndpointMiddleware, endpoint routing, endpoint, MapRazorPages(), Razor Pages, endpoints, route template, URL, dictionary

14.2 Convention-based routing vs. explicit routing

This section contrasts global convention-based routing with endpoint-specific explicit routing and recommends explicit routing, including Razor Pages' convention-generated explicit routes, for most applications.

Key terms: convention-based routing, explicit routing, route template, endpoint, attribute routing, [Route], minimal APIs, MVC controllers, Razor Pages, conventions

14.3 Routing requests to Razor Pages

This section explains how ASP.NET Core conventionally derives routes from Razor Page file paths and routes matching requests to their page handlers.

Key terms: MapRazorPages(), route template, Razor Pages root directory, Pages/, RoutingMiddleware, EndpointMiddleware, endpoint, PageModel, page handler, OnGet

14.4 Customizing Razor Page route templates

This section introduces customizing individual Razor Page route templates to control application URLs and map multiple URLs to one page.

Key terms: Razor Page, route template, file path, literal segment, route parameter, map multiple URLs

14.4.1 Adding a segment to a Razor Page route template

This section shows how to append route segments, especially route parameters, to a Razor Page’s default URL by editing its @page directive.

Key terms: @page directive, route template, route segment, default route template, route parameter, optional parameters, default parameters, constraints, Products/{category}/{name}

14.4.2 Replacing a Razor Page route template completely

This section explains how a leading slash in a Razor Page @page directive completely replaces the convention-based route derived from the page’s location.

Key terms: @page directive, custom route template, absolute route template, literal segments, route template, static custom template, checkout, default route template, Razor Pages conventions

14.5 Generating URLs for Razor Pages

This section introduces routing-based URL generation for Razor Pages and MVC so links remain correct when convention-based page URLs change.

Key terms: routing infrastructure, convention-based routing, Razor Pages, Pages/Cart.cshtml, Pages/Basket/View.cshtml, IUrlHelper, PageModel, LinkGenerator, MVC controllers

14.5.1 Generating URLs for a Razor Page

This section shows how Razor Pages and MVC code generate links to Razor Pages through IUrlHelper.Page using page paths and route values.

Key terms: IUrlHelper, Url, Page(), PageModel, route template, Razor Page, relative file path, absolute file path, Pages folder, route values

14.5.2 Generating URLs for an MVC controller

This section explains how MVC controllers generate URLs with IUrlHelper and how helper choice, ambient route values, and LinkGenerator affect URL generation.

Key terms: IUrlHelper, Action(), controller name, action name, Url helper, Page(), Razor Page, MVC action, ambient values, route values

14.5.3 Generating URLs with LinkGenerator

This section explains when and why to use LinkGenerator to generate Razor Pages and MVC URLs, especially from application code outside an HTTP request.

Key terms: LinkGenerator, IUrlHelper, request context, business logic, domain logic, frontend application design, Dependency Inversion principle, background service, GetPathByPage(), GetPathByAction()

14.6 Customizing conventions with Razor Pages

This section explains how to customize Razor Pages routing conventions globally while preserving convention-based URLs and avoiding ambiguous routes.

Key terms: RouteOptions, LowercaseUrls, AppendTrailingSlash, parameter transformer, kebab-case, AddRazorPages, AddRazorPagesOptions, RazorPagesOptions, Program.cs, AddPageRoute

14.7 Summary

Summarizes how Razor Pages map URLs through convention-generated routes, how those routes can be customized, and how URLs can be generated and configured.

Where this is used later

Concepts explained in this chapter

← Chapter 13: Creating a website with Razor Pages Chapter 15: Generating responses with page handlers in Razor Pages →

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.