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.