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

4.1 Routing basics

 
 
 
 

4.2 Customizing route templates

 
 

4.2.1 Overriding routes

 
 
 

4.2.2 Route Parameters

 
 
 

4.2.3 Binding route data to handler parameters

 
 

4.2.4 Catch-all parameters

 
 

4.2.5 Route Constraints

 

4.2.6 Creating Additional Routes

 
 
 

4.2.7 Working with PageRouteModel conventions directly

 
 
 

4.3 Generating URLs

 
 
 
 

4.3.1 The anchor tag helper

 
 

4.3.2 Using the IUrlHelper to generate URLs

 
 
 
 

4.3.3 Generating redirect URLs from ActionResults

 
 
 

4.3.4 Customizing URL generation

 
 
 
 

4.3.5 Using Parameter Transformers to customize route and parameter value generation

 
 
 

4.4 Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest