5 Mapping URLs to Razor Pages using routing

 

This chapter covers

  • Mapping URLs to Razor Pages
  • Using constraints and default values to match URLs
  • Generating URLs from route parameters

In chapter 4, you learned about the 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 which act as “mini controllers” for a request. The page handler calls the application model to retrieve or save data. The handler then 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 for a given request. This process is called routing and is the focus of this chapter.

This chapter begins by identifying the need for routing and why it’s useful. You’ll learn about the endpoint routing system introduced in ASP.NET Core 3.0, see several examples of routing techniques, and explore the separation routing can bring between the layout of your Razor Page files and the URLs you expose.

The bulk of this chapter focuses on how to use routing with Razor Pages to create dynamic URLs, so that a single Razor Page can handle requests to multiple URLs. I’ll show how to build powerful route templates and give you a taste of the available options.

5.1      What is routing?

5.2      Routing in ASP.NET Core

5.2.1   Using endpoint routing in ASP.NET Core

5.2.2   Convention-based routing vs attribute routing

5.2.3   Routing to Razor Pages

5.3      Customizing Razor Page route templates

5.3.1   Adding a segment to a Razor Page route template

5.3.2   Replacing a Razor Page route template completely

5.4      Exploring the route template syntax

5.4.1   Using optional and default values

5.4.2   Adding additional constraints to route parameters

5.4.3   Matching arbitrary URLs with the catch-all parameter

5.5      Generating URLs from route parameters

5.5.1   Generating URLs for a Razor Page

5.5.2   Generating URLs for an MVC controller

5.5.3   Generating URLs with ActionResults

5.5.4   Generating URLs from other parts of your application

5.6      Selecting a page handler to invoke

5.7      Customizing conventions with Razor Pages

5.8      Summary

sitemap