We have a asp.net core website using Razor without controller and I want to set default page for every sub folder.
For example, the root folder is Pages
, then we have PagesMembers
folder, PagesProducts
folder. While someone is visiting http://www.xxx.xxx/Products
, I want it to show PagesProductsDefault.cshtml
page.
I have tried
app.MapRazorPages();
app.UseDefaultFiles();
It does not work well. How can I implement it? Thanks!
2
Answers
The default page in each subfolder is
Index.cshtml
.To set a different page see: How to configure razorpages to not use Index.cshtml as the default page in folders under the Pages folder
You can use an
IPageRouteModelConvention
(https://www.learnrazorpages.com/advanced/custom-route-conventions) to centralise this without having to remember to apply a route template to each page.Create a class that implements
IPageRouteModelConvention
which enables you to create a convention for generating routes at start up in addition to the default conventions provided by the framework:Register this with the application services:
Depending on your circumstances, you might also need to check that there isn’t an Index page in the same folder. If there is, you will create ambiguous routes for the folder and the framework won’t know which page to call, unless you remove the default template from the Index page’s templates.