aspdotnet_how_URLs_matched.md - brainchildservices/curriculum GitHub Wiki

SLIDE-1

How URLs are matched?

When a Razor Pages application starts up, a collection of Attribute Routes is constructed, using the file and folder paths rooted in the Pages folder as the basis for each route's template.

The standard Razor Pages 3.x site template includes three pages in the root folder:

  Error.cshtml
  Index.cshtml
  Privacy.cshtml

A collection of four routes are defined with the following route templates:

  ""
  "Error"
  "Index"
  "Privacy"

By default, the route templates are generated by taking the virtual path of each content page and then removing the root folder name from the start and the file extension from the end.

SLIDE-2

Index.cshtml is considered the default document in any folder, so it has two routes defined

  • one for the file name without the extension, and
  • one with an empty string representing the file. Therefore, you can access Index.cshtml by browsing to both http://yourdomain.com/ and http://yourdomain.com/index.

If you create a folder named Test and add a file named Index.cshtml to it, a further two routes will be defined with the following templates:

  "Test"
  "Test/Index"

Both of these routes will be mapped to the same virtual path: /<root>/Test/Index.cshtml.

SLIDE-3

However, if you now add a file called Test.cshtml to the root pages folder and attempt to browse to it, an exception will be raised:

  AmbiguousActionException: Multiple actions matched.
  The following actions matched route data and had all constraints satisfied:

  Page: /Test/Index
  Page: /Test

As the exception message says, it is an error to have a single URL mapped to multiple actions or routes. The framework has no way of knowing which page to call. You can disambiguate between routes by adding route parameters and/or constraints to the template.

⚠️ **GitHub.com Fallback** ⚠️