英文:
How to configure razorpages to not use Index.cshtml as the default page in folders under the Pages folder
问题
为了避免在我的代码库中有许多名为Index.cshtml的文件,我想要使用文件名命名为子文件夹并以"Page"作为Razor页面站点中默认页面的后缀,而不是传统的Index.cshtml。
所以,不是使用这种文件结构:
/Pages
/Settings
Index.cshtml
/Users
Index.cshtml
我想要这种文件结构:
/Pages
/Settings
SettingsPage.cshtml
/Users
UsersPage.cshtml
我尝试在SettingsPage.cshtml中设置路由,如下所示:
@page "/"
但这会导致以下错误:
在处理请求时发生未处理的异常。
AmbiguousMatchException: 请求匹配了多个端点。匹配项:
/Index
/Settings/SettingsPage
我该如何配置Razor页面的选项/约定以使用我上面描述的内容?
英文:
To avoid having many files named Index.cshtml in my codebase, I want to use filenames named after the subfolder and with the suffix "Page" as the default pages in a Razor pages site instead of the conventional Index.cshtml.
So instead of having this file structure
/Pages
/Settings
Index.cshtml
/Users
Index.cshtml
I want this file structure:
/Pages
/Settings
SettingsPage.cshtml
/Users
UsersPage.cshtml
I have tried setting the route in SettingsPage.cshtml like this:
@page "/"
But this results in this error:
An unhandled exception occurred while processing the request.
AmbiguousMatchException: The request matched multiple endpoints. Matches:
/Index
/Settings/SettingsPage
How can I configure the razorpages options/conventions to use what I described above?
答案1
得分: 0
I found the solution to be to use the folder name in the @page directive. Instead of having a file at /Pages/Settings/Index.cshtml
with the first line as @page
, I created a file at /Pages/Settings/SettingsPage.cshtml
with the first line as @page "/Settings"
. When linking to the page, I specify the new page file name instead of Index. For example, instead of:
<a class="nav-link text-dark" asp-area="" asp-page="/Settings/Index">Settings</a>
I use:
<a class="nav-link text-dark" asp-area="" asp-page="/Settings/SettingsPage">Settings</a>
This approach seems to work. Any comments or suggestions are still much appreciated.
英文:
Ok, the solution I found was to simply use the folder name in the @page directive like this:
Instead of having this file /Pages/Settings/Index.cshtml
where the first line is
@page
I have this file /Pages/Settings/SettingsPage.cshtml
where the first line is:
@page "/Settings"
And when linking to the page I need to specify the new page file name instead of Index, for instance, ie. instead of:
<a class="nav-link text-dark" asp-area="" asp-page="/Settings/Index">Settings</a>
I use
<a class="nav-link text-dark" asp-area="" asp-page="/Settings/SettingsPage">Settings</a>
This works it seems. Any comments or suggestions are still much appreciated.
答案2
得分: -1
错误指示有多个 Razor 页面 (Pages/Index.cshtml 和 Pages/Settings/SettingsPage.cshtml) 共享相同的路由。
如果你希望 Pages/Settings/SettingsPage.cshtml 匹配路径 "/”,请将 Pages/Index.cshtml 设置为另一个路由。
例如:@page "/Index"
如果你没有修改任何内容,/Pages/Index.cshtml 将匹配路径 "/” 和 "/Index"。你可以查看这个文档,当你指定路径 "/” 给 Pages/Settings/SettingsPage.cshtml 时,就会出现这个错误。
英文:
The error indicates there're Mutipule razor pages (Pages/Index.cshtml&Pages/Settings/SettingsPage.cshtml)share the same route.
If you want Pages/Settings/SettingsPage.cshtml match the path "/",Set Pages/Index.cshtml with another route
For example @page "/Index"
If you didn't modify anything, /Pages/Index.cshtml would match the path"/" and "/Index" You could check this document ,When you specific the path"/" for Pages/Settings/SettingsPage.cshtml, the error occurs
答案3
得分: -1
尝试将您的SettingsPage.cshtml设置如下:
@page "/SettingsPage"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论