【发布时间】:2021-06-11 12:10:18
【问题描述】:
我们希望将网站从 Asp.Net Webforms 迁移到 Asp.Net Core Webapplication (Razor Pages)。我们现在有没有可见标识符的可读 URL(为了可读性、SEO 和防止在可能的数据库迁移后更改 URL...)。
为此,我在启动时在 Global.Asax 中动态生成了路由:
RouteTable.Routes.MapPageRoute("myroute1",
"banana/",
"~/content.aspx", false, new RouteValueDictionary { { "id", "4711" }, , { "otherid", "4812" }
RouteTable.Routes.MapPageRoute("myroute2",
"apple/",
"~/content.aspx", false, new RouteValueDictionary { { "id", "4913" }, , { "otherid", "5014" }
这样用户就可以像这样调用 content.aspx:
https://example.com/banana
https://example.com/apple
在 content.aspx 中,我从 RouteValues 中获得了映射的“id”和“otherid”。
如何在 Razor Pages 中实现这一点?
现在我有一个“Content.cshtml”,我需要访问 id 和 otherid。我在顶部添加了这个:
@page "/content/{id:int}/{otherid:int}/{title:string}"
上面的代码允许 mit 像这样调用 Urls:
https://example.com/content/4711/4812/banana // still have the ids in it and prefix "content"
是否有可能在启动时使用固定参数添加所有路由?我找不到类似的东西。
问候 cpt.oneeye
【问题讨论】:
标签: c# asp.net asp.net-core routes razor-pages