14 Mapping URLs to Razor Pages using routing

 

This chapter covers

  • Routing requests to Razor Pages
  • Customizing Razor Page route templates
  • Generating URLs for Razor Pages

In chapter 13 you learned about the Model-View-Controller (MVC) design pattern and how ASP.NET Core uses it to generate the UI for an application using Razor Pages. Razor Pages contain page handlers that act as mini controllers for a request. The page handler calls the application model to retrieve or save data. Then the handler passes data from the application model to the Razor view, which generates an HTML response.

Although not part of the MVC design pattern per se, one crucial part of Razor Pages is selecting which Razor Page to invoke in response to a given request. Razor Pages use the same routing system as minimal APIs (introduced in chapter 6); this chapter focuses on how routing works with Razor Pages.

I start this chapter with a brief reminder about how routing works in ASP.NET Core. I’ll touch on the two pieces of middleware that are crucial to endpoint routing in .NET 7 and the approach Razor Pages uses of mixing conventions with explicit route templates.

In section 14.3 we look at the default routing behavior of Razor Pages, and in section 14.4 you’ll learn how to customize the behavior by adding or changing route templates. Razor Pages have access to the same route template features that you learned about in chapter 6, and in section 14.4 you’ll learn how to them.

14.1 Routing in ASP.NET Core

14.2 Convention-based routing vs. explicit routing

14.3 Routing requests to Razor Pages

14.4 Customizing Razor Page route templates

14.4.1 Adding a segment to a Razor Page route template

14.4.2 Replacing a Razor Page route template completely

14.5 Generating URLs for Razor Pages

14.5.1 Generating URLs for a Razor Page

14.5.2 Generating URLs for an MVC controller

14.5.3 Generating URLs with LinkGenerator

14.6 Customizing conventions with Razor Pages

sitemap