如何使用 Razor 页面标签助手路由到嵌套区域

问题描述 投票:0回答:1

环境:
Visual Studio 2019 16.5.4
.NET Core 3.1
剃刀页面

我有一个嵌套区域,目录结构如下所示:

/Areas
/Areas/customer
/Areas/customer/Pages
/Areas/customer/Pages/index.cshtml
/Areas/customer/Pages/create.cshtml
/Areas/customer/Pages/note
/Areas/customer/Pages/note/index.cshtml
/Areas/customer/Pages/note/create.cshtml

我正在尝试找出如何使用

note
asp-page
标签助手路由到
asp-area
的嵌套区域。我从未在锚标记的
href
部分获得任何内容。这是我正在使用的 Razor Pages 语法(我基于 Identity 脚手架代码):

<a class="btn btn-primary btn-sm" asp-area="customer" asp-page="/note" asp-route-CustomerId="@item.Id"><span class="btn-label-sm"><i class="fas fa-sticky-note"></i></span>Notes</a>

我尝试了很多不同的

asp-page
asp-area
组合,但没有成功。如果我自己生成
href
它可以工作:

<a class="btn btn-primary btn-sm" href="/customer/[email protected]"><span class="btn-label-sm"><i class="fas fa-sticky-note"></i></span>Notes</a>

我只是想知道为什么标签助手不起作用,以及我是否在嵌套区域或配置路线方面做错了什么。

非常感谢任何帮助或见解。谢谢。

asp.net asp.net-core razor razor-pages
1个回答
0
投票

如果您在 cshtml 中的

@page
声明具有路由参数,例如
@page {id}
并且该参数没有像
{id?}
那样标记为可为空,那么您 必须 将其提供给锚标记助手,如
<a asp-page="pagename" asp-route-id="123">

(在帮助程序的代码中,您尝试了此操作,但使用“CustomerId”,而手动编写的锚标记的 href 具有“id”,因此在您的情况下可能只是一个拼写错误)

© www.soinside.com 2019 - 2024. All rights reserved.