【问题标题】:WebApi 2 route with parameter in path and queryWebApi 2 路由在路径和查询中带有参数
【发布时间】:2018-12-28 01:24:44
【问题描述】:

我最近开始将现有 Web 服务从 WCF 迁移到 ASP.NET WebApi 2。其中一个遗留端点有点奇怪,我在弄清楚如何在 WebApi 控制器中复制它时遇到了问题。问题是端点具有与路径的一部分同名的查询参数。

在 WCF 中,我们有...

[WebGet(UriTemplate = "configuration/id?id={id}")]
Config GetConfigByID(string id);

我尝试在控制器中复制此内容,但客户端收到 404 错误。

[Route("configuration/id")
public IHttpActionResult GetConfigByID(string id)
{
    Config config = GetConfig(id);
    return Ok(config);
}

尽管我想更改端点,但我需要它与现有客户端一起工作。

【问题讨论】:

    标签: c# asp.net-web-api routes asp.net-web-api2


    【解决方案1】:
    [Route("configuration/id")
    public IHttpActionResult GetConfigByID([FromUri] string id)
    {
        Config config = GetConfig(id);
        return Ok(config);
    }
    

    你可以试试FromUri属性

    【讨论】:

      【解决方案2】:

      没关系。我的确切问题非常好。控制器中的其他东西是胖手指。继续……

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-21
        • 2017-04-03
        • 2018-01-04
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多