【问题标题】:Cannot call HttpGet method with parameters in C# .Net Framework无法在 C# .Net Framework 中调用带有参数的 HttpGet 方法
【发布时间】:2021-05-02 06:15:11
【问题描述】:

我在调用带参数的 HttpGet 方法时遇到问题。我在 .Net Framework 4.7.2 中工作。我正在使用 Advanced REST Client 来测试我的方法。 当我调用一个不带参数的方法时,它工作得很好,但是当我想调用一个带参数的方法时,我不能。我犯的错误在哪里? 这是我的两种方法。

[HttpGet]
    [Route("action1")]
    public IHttpActionResult action1()
    {
        return Ok("blabla");
    }

    [HttpGet]
    [Route("action2/{parameter}")]
    public IHttpActionResult action2(string parameter)
    {
        return Ok(parameter);
    }

这就是我所说的第二种方法。正如我所说,第一个工作得很好。

这是我不断收到的消息:{ "Message": "No HTTP resource was found that matches the request URI 'https://localhost:44338/api/login/action2/?parameter="blabla"'.", "MessageDetail": "No action was found on the controller 'Login' that matches the request." }

【问题讨论】:

  • 尝试使用网址https://localhost:44338/api/login/action2/blabla
  • 哦,谢谢,确实有效。我以为我试过了,但显然只用引号试过了
  • 仅供参考 - 查询参数中的字符串也不需要引号。

标签: c# http-get


【解决方案1】:

该操作使用了一个路由参数,该参数神奇地将部分 url 字符串(不是查询参数)与操作参数匹配。

您应该将 Postman 中的 URL 更改为 https://localhost:44338/api/login/action2/blabla

或者你可以从路由属性中删除{parameter}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 2015-05-13
    相关资源
    最近更新 更多