I'm new to razor pages (used to MVC pattern). I came across some interesting routing pattern I saw on GitHub:
services.AddMvc()
.AddViewLocalization()
.AddDataAnnotationsLocalization()
.AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Edit", "/{Slug}/Edit");
options.Conventions.AddPageRoute("/Delete", "{Slug}/Delete");
options.Conventions.AddPageRoute("/Details", "{Slug?}");
options.Conventions.AddPageRoute("/Details", #"Index");
options.Conventions.AddPageRoute("/Create", "{Slug?}/Create");
});
In some projects, I don't see the AddRazorPagesOption
Just out of curiosity, what would happen if I didn't specify routing like this?
The method options.Conventions.AddPageRoute just help you to define custom routing for your pages. So, the razor page:
Edit will have route template /{Slug}/Edit where Slug is a parameter from the URL.
Delete will have route template /{Slug}/Delete where Slug is a parameter from the URL.
Details will have route template /{Slug?} where Slug is a optional parameter from the URL. Also you can go to that page using /Index.
Create will have route template /{Slug?}/Create where Slug is a optional parameter from the URL.
If you remove them all your razor pages will just use the defaut conventions which is folder base convention started the Pages folder as the root folder. For pages that have parameters you should pass them as query parameter in your request URL. For example without the conventions defined you'll have to use /Edit?slug=myValue if you want to go to the Edit page of myValue item. With the convention configured, the URL is pretty because you'll use /myValue/Edit.
I think the configuration you actually have is just here to avoid having parameters of your razor pages to be passed as query parameter in your request URL.
Related
By default, nuxt adds a route for each page in pages.
I want to make when going to the page e.g. project.local/id/ec29cjsa5fas512ik, the user goes to a page template and the Vue receives this ec29cjsa5fas512ik id from the url so it can make proper API calls later.
You can make a dynamic page in Nuxt2 with the following file, eg.
/pages/details/_id.vue
then you'll have a path like /details/:id.
More info can be found here: https://nuxtjs.org/docs/features/file-system-routing#dynamic-routes
I what to use the same razor page in more than one position in my web app.
I have the page /one and I want to reach also with /two and /three.
I can do it with the AddPageRoute :
options.Conventions.AddPageRoute("/one", "two");
options.Conventions.AddPageRoute("/one", "three");
But the taghelpers in the page always render formaction="/three?handler=Update" pointing to the last route, same for /one, /two or /three
This make impossible use the same razor page in different position in my web application.
Git sample : https://github.com/enricoe73/OnePage2Routes.git
You can reach the page using any of the routes you defined, but the outbound URL generation components in the framework (tag helpers, IUrlHelper etc) will always use the last route registered when generating an outbound URL, so the order in which the routes are registered plays a part.
If that doesn't suit you (because you may want to point to different routes at different times, for instance), the solution is to use the standard HTML formaction attribute instead of the custom asp-* attributes that belong to the tag helper:
<button formaction="/two?handler=update">Update</button>
Say I have a RazorPages application with pages in folders under the Pages folder like in the image below:
pretty standard. I want this application to be available for different clients so that they use different URLs to access it. I can use a #page "/RRate/Index/{sName}" directive in the RRate\Index.cshtml file so that the URL <root>\RRate\Index\client_namewill bind client_name to sName.
My question is: Can I use a URL like <root>\client_name\RRate\Index so that the client name is the first part of the route? How would I implement a route like that?
Not surprisingly, a #page "/{sName}/RRate/Index" directive does not work.
I am using .NET Core 3.1, services.AddRazorPages(); and endpoints.MapRazorPages(); in startup.
I have looked at endpoints.MapDynamicControllerRoute() but this approach does not seem to mesh with Razor pages. Am I wrong?
Well the answer was in the article mentioned by #mj1313.
I added PageRoutes for each page (I wander if there is a way to do this in one route) like the following: options.Conventions.AddPageRoute("/RRate/Index", "{survey}/RRate/Index");.
I'm using Asp.Net Core 3.1 Razor pages. I need to override the the route for one of the page in my area.
I have Home page in my Area - Blog and the current route is localhost/blog/home. I need to change this to localhost/blog. I have an Index page in the same blog area but it has an route parameter. So it will be localhost/blog/my-blog-name. And this will not interfere with localhost/blog
I added the setting in ConfigureServices in startup.cs
services
.AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaPageRoute("Blog", "/", "/blog/home");
});
Here is my folder structure,
But when i navigate to localhost/blog I get an 404 not found. Please assist on where I'm wrong.
I have Home page in my Area - Blog and the current route is localhost/blog/home. I need to change this to localhost/blog. I have an Index page in the same blog area but it has an route parameter.
when i navigate to localhost/blog I get an 404 not found.
If you check the definition of AddAreaPageRoute method, you can find it takes the name of the area, the name of the page, and the route template, like below.
.AddAreaPageRoute("{area_name_here}", "{page_name_here}", "{route_here}")
Based on your requirement and code, we can find you do not configure the specified route to the page correctly, and if your Index page of Blog area accepts a required (not optional) route parameter, which would cause 404 error while you browse localhost/blog.
To fix the issue and achieve the requirement, you can try modify the code like below.
services.AddRazorPages()
.AddRazorPagesOptions(
options =>
{
options.Conventions.AddAreaPageRoute("Blog", "/home", "/blog");
}
);
Test Result
I've seen in some sample code that a route template ("{id:int}") on top of razor page causes the links to that page to use another pattern:
https://localhost/Movies/Edit/6
instead of
https://localhost/Movies/Details?id=6
My question is how asp.net manages to change all the links to that pattern, does it know about that page before rendering it?
Does it collaborate with other pages when processing a page?
When the application first starts, a collection of attribute routes are built. The routes are built for any Razor file with an #page directive in the root Pages folder, and for any other routes that have been defined via PageRouteConventions.
When you use the Url helper to generate links, or the anchor tag helper (which uses the Url helper behind the scenes), the link that gets generated is based on the attribute route that was built for the page that you pass to the helper.
In attribute routing, route parameters are added as segments in the URL, which is why the values are not appended as query string values. If you prefer query strings, don't declare route values as part of the #page directive.
Run the dotnet publish -c Release command and take a look inside the bin/Release folder.
You will not find your .cshtml files with html in them. What happaned where did all the html go? And how does this relate to the question?
You gotta remember that cshtml will endup being your regular ol' c# and all that fancy razor templating syntax end's up being c#. This process has many names and transpilation is one of them performed by transpilers.
Okey so now that we can safely assume that when you have a Index.cshtml file it will get populated in to some sort of an object, let's call it RazorPage.cs this will just store all the configuration for this page. Now let's say this index page is living in a folder called Home now we can have a dictionary Dictionary<string, RazorPage> and let's say that the key will be "/Home/Index". Following along based on transpiled #page "{id:int}" syntax, it might generate a template string for the route and store that in the RazorPage in a RouteTemplate parameter.
So when you use asp-page tag helper it will find the correct RazorPage and it can know the template for the url, populating it with the values you provided.
I haven't seen the actual implementation this is just my guess.
My question is how asp.net manages to change all the links to that pattern, does it know about that page before rendering it?
Yes it knows everything about the page at run time. Most likely the services.AddMvc() service takes care of loading in all the razor pages / views / controllers, at startup.
Does it collaborate with other pages when processing a page?
Highly likely no, unless you mean components/layouts/partials. It will however struggle to resolve a page if you have identical route for 2 pages.