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—for example, OnGet, OnPost, and so on. Before the handler selection process can take place, the correct page must be selected. 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).

Many 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 of a web application that provides vacation packages from the last chapter, table 4.1 provides some examples of this one-to-one match between some imagined segmented URLs and their file paths in a web application.

Table 4.1 It is common to find a one-to-one mapping between incoming URLs and page file paths. (view table figure)

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

4.1 Routing basics

4.1.1 Route templates

4.2 Customizing route templates

4.2.1 Overriding routes

4.2.2 Route parameters