在 _layout 和索引页面之间的 asp.net 核心剃须刀页面中使用 asp-page 和 asp-route 的正确方法
Right way of using asp-page and asp-route in asp.net core razor pages between _layout and index page
下面是文件结构
我在 Pages 文件夹下有默认的 Index page,其布局为 _LayoutHarman.cshmtl
代码:Pages/Shared/_LayoutHarman.chtml
header 菜单:在这种情况下,页面位于 subfolder.ie 类别文件夹中
<a asp-route-cat_slug="electronic" asp-page="./category/Index">Electronic</a>
<a asp-route-cat_slug="beauty-care" asp-page="./category/index" >Beauty Care</a>
<a asp-route-cat_slug="toy" asp-page="./category/index" >Toy</a>
页脚菜单:页面在根文件夹中
<a asp-page="./Contact" >Contact</a>
<a asp-page="./terms" >Terms</a>
<a asp-page="./privacy" >Privacy</a>
代码:Pages/category/Index.cshtml
@page "{cat_slug?}/{pageIndex:int?}"
@model bList.Pages.category.IndexModel
@{
}
<nav aria-label="Page navigation example">
<ul class="pagination text-center">
@if(Model.prev_no!=0){
<li><a asp-page="category" asp-route-cat_slug="@Model.cat_url" asp-route-pageIndex="@Model.prev_no"> Previous</a></li>
}
@if (Model.no_more_record != "YES")
{
<li><a asp-page="category" asp-route-cat_slug="@Model.cat_url" asp-route-pageIndex="@Model.next_no">Next</a></li>
}
</ul>
</nav>
这里下一个/上一个按钮生成 url 如下
https://localhost:8080/category/toy/1
https://localhost:8080/category/toy/2
https://localhost:8080/category/toy/3
相应的所选类别
问题: 当我访问 类别页面 并单击上一个或下一个按钮,然后尝试单击 link Contact,Terms,Privacy 即(在 _LayoutHarman.cshtml 上)或在 header 菜单上,然后 href 变为 空白。
编辑一:
代码:开_LayoutHarman.cshtml
页眉菜单:
<a href="./category/toy" >toy</a>
页脚菜单
<a asp-page="./Contact" >Contact</a>
<a asp-page="./terms" >Terms</a>
代码:在 Category/Index.html 页面上
<a href="/category/@Model.cat_url/@Model.prev_no">Prev</a></li>
<a href="/category/@Model.cat_url/@Model.next_no">Next</a>
现在单击 下一个/上一个 按钮,header 菜单生成 url 作为
https://localhost:44382/category/toy/category/toy 因此出现错误页面。但对于页脚菜单 contact/term/privacy 正常工作
asp-route-cat_slug asp-页面,您真的需要所有这些额外的服务器端处理吗?
不能只使用普通的老式 HTML 超链接吗?至于动态参数,也许你可以只设置它们...
<a href="/category/toy/@Model.GetCategoryID">View category</a>
只是一个想法……
干杯
我确实重现了你的问题,你需要在 _LayoutHarman.cshmtl
的 asp-page
属性中 delete the point(.)
。
改成这样:
<a asp-route-cat_slug="electronic" asp-page="/category/Index">Electronic</a>
<a asp-route-cat_slug="beauty-care" asp-page="/category/index" >Beauty Care</a>
<a asp-route-cat_slug="toy" asp-page="/category/index" >Toy</a>
<a asp-page="/Contact" >Contact</a>
<a asp-page="/terms" >Terms</a>
<a asp-page="/privacy" >Privacy</a>
并且Pages/category/Index.cshtml
中不需要添加asp-page="category"
,只需要在本页中删除该属性即可。
这是我的测试结果:
下面是文件结构
我在 Pages 文件夹下有默认的 Index page,其布局为 _LayoutHarman.cshmtl
代码:Pages/Shared/_LayoutHarman.chtml
header 菜单:在这种情况下,页面位于 subfolder.ie 类别文件夹中
<a asp-route-cat_slug="electronic" asp-page="./category/Index">Electronic</a>
<a asp-route-cat_slug="beauty-care" asp-page="./category/index" >Beauty Care</a>
<a asp-route-cat_slug="toy" asp-page="./category/index" >Toy</a>
页脚菜单:页面在根文件夹中
<a asp-page="./Contact" >Contact</a>
<a asp-page="./terms" >Terms</a>
<a asp-page="./privacy" >Privacy</a>
代码:Pages/category/Index.cshtml
@page "{cat_slug?}/{pageIndex:int?}"
@model bList.Pages.category.IndexModel
@{
}
<nav aria-label="Page navigation example">
<ul class="pagination text-center">
@if(Model.prev_no!=0){
<li><a asp-page="category" asp-route-cat_slug="@Model.cat_url" asp-route-pageIndex="@Model.prev_no"> Previous</a></li>
}
@if (Model.no_more_record != "YES")
{
<li><a asp-page="category" asp-route-cat_slug="@Model.cat_url" asp-route-pageIndex="@Model.next_no">Next</a></li>
}
</ul>
</nav>
这里下一个/上一个按钮生成 url 如下
https://localhost:8080/category/toy/1
https://localhost:8080/category/toy/2
https://localhost:8080/category/toy/3
相应的所选类别
问题: 当我访问 类别页面 并单击上一个或下一个按钮,然后尝试单击 link Contact,Terms,Privacy 即(在 _LayoutHarman.cshtml 上)或在 header 菜单上,然后 href 变为 空白。
编辑一:
代码:开_LayoutHarman.cshtml
页眉菜单:
<a href="./category/toy" >toy</a>
页脚菜单
<a asp-page="./Contact" >Contact</a>
<a asp-page="./terms" >Terms</a>
代码:在 Category/Index.html 页面上
<a href="/category/@Model.cat_url/@Model.prev_no">Prev</a></li>
<a href="/category/@Model.cat_url/@Model.next_no">Next</a>
现在单击 下一个/上一个 按钮,header 菜单生成 url 作为 https://localhost:44382/category/toy/category/toy 因此出现错误页面。但对于页脚菜单 contact/term/privacy 正常工作
asp-route-cat_slug asp-页面,您真的需要所有这些额外的服务器端处理吗? 不能只使用普通的老式 HTML 超链接吗?至于动态参数,也许你可以只设置它们...
<a href="/category/toy/@Model.GetCategoryID">View category</a>
只是一个想法……
干杯
我确实重现了你的问题,你需要在 _LayoutHarman.cshmtl
的 asp-page
属性中 delete the point(.)
。
改成这样:
<a asp-route-cat_slug="electronic" asp-page="/category/Index">Electronic</a>
<a asp-route-cat_slug="beauty-care" asp-page="/category/index" >Beauty Care</a>
<a asp-route-cat_slug="toy" asp-page="/category/index" >Toy</a>
<a asp-page="/Contact" >Contact</a>
<a asp-page="/terms" >Terms</a>
<a asp-page="/privacy" >Privacy</a>
并且Pages/category/Index.cshtml
中不需要添加asp-page="category"
,只需要在本页中删除该属性即可。
这是我的测试结果: