【问题标题】:How to match multiple routes in Razor Pages?如何匹配 Razor Pages 中的多个路由?
【发布时间】:2022-01-07 19:41:39
【问题描述】:
我需要为两个不同的 URL 显示一个表单:
/customer/edit/15
或
/customer/edit/sales/john
我想为他们两个显示相同的表格。要么提供客户 ID,要么提供部门名称和员工姓名。
如何在我的页面路由中实现这一点?
我已经有了这个:
@page /customer/{action}/{id?:long}
【问题讨论】:
标签:
c#
asp.net-core
routes
razor-pages
【解决方案1】:
您可以尝试使用options.Conventions.AddPageRoute。
这是一个演示:
页面/客户/Edit.cshtml:
@page "/customer/edit/{id:long}"
@model RazorPageDemo.Pages.Customer.EditModel
@{
}
<h1>edit</h1>
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
...
services
.AddRazorPages()
.AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/customer/edit", "/customer/edit/{department}/{employee}");
});
}
结果: