4 Matching URLs to Razor Pages with routing
This chapter covers
- Evaluating how URLs are matched to Razor pages
- Examining the use of Route Templates to control the matching process
- Working with data passed in URLs
- Overriding conventional routes
- Generating outbound URLs
In the last chapter, we looked at how to influence which handler method is invoked for a particular request by incorporating an HTTP method name into the name of a handler method: OnGet, OnPost and so on. Before the handler selection process can take place, the correct page must be selected first. This chapter focuses on Routing, which is the process that maps the incoming request to a specific page, or endpoint (incoming URLs) and generates URLs that map to those endpoints (outgoing URLs).
A lot of web application frameworks map URLs to a web page’s file path on disk, based on matching the URL segments to a folder structure. Continuing the theme from the last chapter that revolved around a web application that provides short holidays, table 4.1 provides some examples of this one-to-one match between some imagined segmented URLs and their file path in a web application:
Table 4.1 It is common to find a one-to-one mapping between incoming URLs and page file paths
Incoming URL |
Maps to |
https://domain.com/city/london |
c:/website/city/london.cshtml |
https://domain.com/booking/checkout |
c:/website/booking/checkout.cshtml |
https://domain.com/login |
c:/website/login.cshtml |