【问题标题】:ASP.NET Routing - With and without a slash at the endASP.NET 路由 - 结尾有和没有斜线
【发布时间】:2011-04-18 20:11:45
【问题描述】:

考虑以下服务合同:

[WebGet(UriTemplate = "/stores")]
DTO.Stores GetAllStores();

[WebGet(UriTemplate = "/stores/{name}")]
DTO.Stores GetStores(string name);

我可以访问这两个网址:http://localhost/v1/storeshttp://localhost/v1/stores/Joe。但是,Url http://localhost/v1/stores/(请注意末尾的斜杠)返回“未找到端点”错误。理想情况下,我希望http://localhost/v1/stores/ 调用 GetAllStores()。

我该怎么做?谢谢!

【问题讨论】:

    标签: asp.net wcf asp.net-mvc-routing


    【解决方案1】:

    我会尝试加入波浪号。也许是“~/stores”?

    或者,使用路由,将“/”放在前面。

    【讨论】:

      【解决方案2】:

      如果使用“string?name”作为参数呢?

      [WebGet(UriTemplate = "/stores/{name}")]
      DTO.Stores GetStores(string? name);
      

      而且由于您拥有的两种方法都返回相同的东西 (DTO.Stores),您可以使用一个方法来获取 Stores,而不是两个(就像您现在所做的那样)。像这样:

      [WebGet(UriTemplate = "/stores/{name}")]
      DTO.Stores GetStores(string? name)
      {
          if(string.IsNullOrEmpty(name))
          {
              //get specific store
          }
          else
          {
              //get all stores
          }
      }
      

      P.S.:我不确定这是否适用于 WCF,但请尝试一下。 ;-)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-09
        • 2023-04-08
        • 2016-03-27
        • 2023-03-23
        • 1970-01-01
        • 2012-04-05
        • 2015-09-06
        • 2016-03-13
        相关资源
        最近更新 更多