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 islocalhost/blog/home
. I need to change this tolocalhost/blog
. I have anIndex
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