How does Routing work in ASPNET Core Razor Page?
In ASP.NET Core Razor Pages, routing determines how HTTP requests are mapped to specific pages. Unlike MVC, which has controllers and actions, Razor Pages relies on the page-based approach, where each page is a self-contained unit that handles its own requests. Here's how routing works in Razor Pages:
1. Page Model Routing Convention
- Each Razor Page has a default route based on its file location in the project. The structure in the Pages folder maps directly to the URL.
- For example, a page at Pages/Products/Details.cshtml would be accessible via /Products/Details in the browser.
- If the page is at the root, Pages/Index.cshtml, it maps to / by default.
2. @page Directive
- Each Razor Page starts with an @page directive, which turns the file into a Razor Page that can handle HTTP requests directly.
- You can specify a route template within the @page directive to customize the URL pattern.
- For example, @page "{id:int}" in Pages/Products/Details.cshtml would match URLs like /Products/Details/1, with the id route parameter mapped to the page’s OnGet or OnPost method.
3. Route Parameters
- Razor Pages support route parameters, which can be specified directly in the @page directive.
- For instance, @page "{category}/{id:int}" allows a URL like /Products/Electronics/1.
- The parameters specified in the URL are automatically bound to handler method parameters or properties with the same name.
4. Handler Methods
- Razor Pages use handler methods (OnGet, OnPost, OnPut, etc.) to handle requests based on HTTP verbs.
- You can add custom handler methods using naming conventions like OnGetDetails or OnPostSubmit. These handlers can then be accessed by appending ?handler=Details to the URL (/Products/Details?handler=Details).
5. Route Customization with AddPageRoute
ASP.NET Core allows customizing routes by using AddPageRoute in the Startup.cs file.
For example:
services.AddRazorPages().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Products/Details", "Shop/Product/{id}");
});
This maps /Shop/Product/{id} to the Details.cshtml page.
6. Attribute Routing
While not as common as in MVC, you can add attribute routing to page handler methods to further customize routes, though typically not needed since Razor Pages are already URL-driven based on their file structure.
7. Custom Route Constraints and Route Segments
Razor Pages supports custom route constraints, such as {id:int} for integer validation. You can define multiple segments, constraints, or even optional parameters in the @page directive.
Example Structure
Given a folder structure:
Pages/
└── Products/
└── Details.cshtml
└── List.cshtml
With these pages:
- Details.cshtml using @page "{id:int}" would map to URLs like /Products/Details/1.
- List.cshtml with a simple @page would map to /Products/List.
Each page can now respond directly to requests, handle route parameters, and use handler methods to process different actions. This approach allows Razor Pages to be structured more like a RESTful resource, making it suitable for page-centric applications.
No comments:
Post a Comment