【问题标题】:Razor Pages ASP.Net Core 2.0 Routing - Is possible to have a parameters in the middle of a URL?Razor Pages ASP.Net Core 2.0 Routing - URL 中间是否可以有参数?
【发布时间】:2017-12-16 12:44:59
【问题描述】:

是否可以在 Razor 页面的 URL 中间有一个参数?

例子:

文件结构:

  • /products/Index
  • /产品/产品/索引
  • /产品/产品/第1页
  • /Products/Product/Page2
  • /Products/Product/ListOfAccessories/Index
  • /Products/Product/ListOfAccessories/Details

我可以为产品列表创建路由,但无法使用 options.Conventions.AddPageRoute

找出 URL 中间的参数

谢谢

【问题讨论】:

    标签: asp.net razor url-routing asp.net-core-2.0


    【解决方案1】:

    您可以像在控制器路由中一样添加约定和使用参数。

     options.Conventions.AddPageRoute("/Products", "products/{id}/details")
     options.Conventions.AddPageRoute("/Products", "products/{id}/listofaccesories")
    

    或者更一般的方式

     options.Conventions.AddPageRoute("/Products", "products/{id}/{handler}")
    

    {handler} 参数将被 razor 页面引擎用于查找映射此路由的方法。 这两个配置会将 URL /products/20120/details 路由到 ProductsModel 页面,将 20120 作为 id 参数并在 ProductsModel 中选择 Details 处理程序.

    public ProductsModel : PageModel
    {
        ...
    
       public Task OnGetDetails(int id)
        {
        ...
        }
    
        public Task OnGetListOfAccessories(int id)
        {
           ...
        }
    
       ...
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 2018-10-29
      • 2018-05-09
      • 1970-01-01
      • 2018-03-12
      • 2020-11-21
      • 1970-01-01
      • 2020-04-04
      • 2019-06-28
      相关资源
      最近更新 更多