我的页面有这个指令@page "/sales/reports"
,所以它在https://localhost/sales/reports
路由加载。
我想创建<a href="">Report 1</a>
一个指向子页面的链接/sales/reports/{id}
——例如https://localhost/sales/reports/8c5b8c1c-12b5-43ee-92c9-8d3bcb8644a4
,但不确定我在锚的 href 中放了什么。当我简单地把<a href="@myGuid">Report 1</a>
链接有一种形式时https://localhost/8c5b8c1c-12b5-43ee-92c9-8d3bcb8644a4
。我需要硬编码<a href="sales/reports/@myGuid">Report 1</a>
还是有更聪明的方法?
术语“子页面”实际上并不存在。
只有“页面”,它们有“路线”。有时路线会出现嵌套,如您的示例所示,但这并不能使其成为“子页面”。
你的问题的答案是,是的,你需要包括你想去的“页面”的路线。
<a href=@($"sales/reports/{@myGuid}")>Report 1</a>
这并不一定意味着页面本身的硬编码。你可以创建一个帮助类来为你做这件事。例如:
class ReportPathCreator
{
public static string ReportRouteFor(Guid reportGuid) => $"sales/reports/{reportGuid}";
}
然后使用:
<a href=@(ReportPathCreator.ReportRouteFor(myGuid))>Report 1</a>
然后,如果您选择更改托管报告的路径,您只需在帮助程序类中进行更改,您网站中的所有锚元素都会自动选择该新路径。
更新
您还可以使用静态类生成模板供您使用,而不是 @page 指令:
class ReportPathCreator
{
public static string ReportRouteFor(Guid reportGuid) => $"sales/reports/{reportGuid}";
public static string ReportTemplate = "sales/reports/{reportGuid:guid}";
}
然后,而不是 @page 指令,使用:
@attribute [Route(ReportPathCreator.ReportTemplate)]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句