我正在开发 .NET Core 2.1 Razor Pages 应用程序,无法弄清楚为什么路由器不符合我的预期。在我的应用程序中,Orchestration 对象具有一组 Rules 对象。 Orchestration 定义为在 DB 中使用 Guid 查询。
Orchestration 文件夹中我的 Index.cshtml 看起来像
@page "{id:guid}"
@model Website.Pages.Orchestration.OrchestrationModel
并且可以预见 localhost/Orchestration/{guid}
路由到正确的页面。
我的目标是 localhost/Orchestration/{guid}/rule/{ruleId}/edit
命中 Rule 文件夹中的 Edit.cshtml。我的尝试很接近,但 .NET 匹配的是 Orchestration/rule
而不是 Orchestration/{guid}/rule
。我尝试了几种尝试将 {orchestrationid}/{id}
添加到 Rule 目录中的 Edit.cshtml 的方法,但它不起作用。这似乎是可能的,我很感激对我遗漏的任何帮助。
注意 rule/Index.cshtml 已重命名为 Edit
Edit.cshtml 文件看起来像
@page "{id}"
@model Website.Pages.Orchestration.Rule.RuleModel
不起作用的部分是尝试访问子路由
最佳答案
您真正想要做的是覆盖依赖于将 URL 与文件路径匹配的默认路由系统。您可以通过指定 override route template 来执行此操作作为 @page
指令的一部分。覆盖路由模板以选项波浪号 (~
) 和正斜杠为前缀,因此以下内容应该适合您:
@page "/Orchestration/{id:guid}/rule/{ruleId:int}/edit"
关于c# - Razor Pages .NET Core 2.1 子路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54640942/