首页 > 解决方案 > Razor 页面中基于 URL 参数的路由

问题描述

这是一个非常简单的问题,但我的搜索没有得到预期的答案。我有一个网址:

http://localhost/customers

显然,这映射到“客户”文件夹中的 Index.cshtml 文件。

我想要另一个网址:

http://localhost/customers/<CustomerID>

映射到另一个页面 /Customers/Detail.cshtml

如何在 .NET Core 3.0 中配置路由,以便将请求http://localhost/customers/<CustomerID>路由到 Detail.cshtml?

标签: asp.net-coreroutingrazor-pages

解决方案


在您的Details.cshtml文件中使用覆盖路由:

@page "/customers/{id:int}"

您可以在此处阅读 Razor 页面中的覆盖路由:https ://www.learnrazorpages.com/razor-pages/routing#override-routes


推荐阅读