aspdotnet_routing.md - brainchildservices/curriculum GitHub Wiki
SLIDE-1
ASP.NET ROUTING
SLIDE-2
Routing is the system that matches URLs to Razor pages. Like most page-centric frameworks, the primary routing system in ASP.NET Razor Pages is based on matching URLs to file paths, starting from the root Razor Pages folder, which is named Pages by default.
When a user request URLs from the server then URLs are handled by the routing system. The Routing system try to find out the matching route pattern of requeted Url with already registered routes which are map to controller, actions, files, or other items. If there is a matching route entry, then it process the request i.e. serve the resource, otherwise it returns 404 error.
SLIDE-3
The @Page Directive
Unlike in either convention-based routing or MVC attribute routing, routes for individual Razor Pages are defined in the CSHTML file of the page itself using the @page attribute.
@page "/index"
The string value in this declaration represents is the "route template" to this page.
SLIDE-4
Route Templates
Route Data parameters are defined in a Route Template as part of the @page directive in the .cshtml file. To cater for the title value in the example above, the declaration at the top of the PageName.cshtml file will look like this:
@page "{title}"
The template created for this route is "PageName/{title}". The {title} part of the template is a placeholder that represents any value added to the URL after post/. The template definition must appear in double quotes, and the parameter must be enclosed in curly brackets or braces.
In this example, the value is required, so you cannot just browse to /post. You must provide a value in the URL to match the "title" segment, otherwise you will get a status code of 404 - Not Found. However, you can make the parameter optional by adding a ? after it:
@page "{title?}"
SLIDE-4(DOWNWARDS)
Or you can provide a default value for the parameter:
@page "{title=first post}"
-
The following words are reserved and cannot be used as names for route param
- action
- area
- controller
- handler
- page
-
REF LINK:: [DO NOT INCLUDE IN SLIDEDECK]