无法在 C# .Net Framework 中使用参数调用 HttpGet 方法

问题描述

我在调用带参数的 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);
    }

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

Advanced Rest client usage

这是我不断收到的消息:{ "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." }

解决方法

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

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

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