【问题标题】: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}");
                   
                });
                
            }
    

    结果:

    【讨论】:

      猜你喜欢
      • 2019-03-25
      • 2018-12-05
      • 2020-03-24
      • 2018-07-18
      • 2019-07-05
      • 2019-01-29
      • 2019-11-29
      • 2011-08-12
      • 2019-07-02
      相关资源
      最近更新 更多