c# - Razor Pages .NET Core 2.1 子路由

标签 c# razor razor-pages

我正在开发 .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

不起作用的部分是尝试访问子路由

enter image description here

最佳答案

您真正想要做的是覆盖依赖于将 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/

相关文章:

asp.net-mvc - Razor View 引擎 .cshtml 页面中的 OutputCache

asp.net-mvc-3 - 如何使用 Asp.Net MVC 3 和 Razor 获得特定于 View 的 <head> 内容?

c# - asp.net core 2.0 dotvvm vs razor pages - 对于交互式网络应用程序来说,哪个更强大?

c# - 反序列化一个对象,一个值没有被反序列化

c# - 此 List<List> 中有什么不正确的地方?

c# - 为 OLEDB Access 数据库使用企业库

c# - 将模型传递给部分时出现类型转换错误

asp.net-mvc - Html.RenderAction MVC 区域路由问题

c# - 对象引用未设置为对象 ViewData ["Title"] = "Index"的实例;

asp.net-core - 处理 Razor 页面异常