通过GetAsync()传递参数

时间:2018-12-03 17:11:50

标签: c# unit-testing asp.net-core asp.net-core-mvc

我有一个要测试的API。这是一种方法的签名...

[HttpGet("{param}")]
 public async Task<IActionResult> Get(string param, string param2)
{
...
}

在测试项目中,我以这种方式构造调用...

        HttpClient client = new HttpClient();
        string uri = "http://localhost:63779/api/controller_name/param/";
        HttpResponseMessage response = await client.GetAsync(uri);

param是路由的一部分,但是如何获取方法的param2?

1 个答案:

答案 0 :(得分:2)

param2作为查询字符串传递给服务器。客户端代码:

HttpClient client = new HttpClient();
string uri = "http://localhost:63779/api/controller_name/param/?param2=SOME_VALUE";
HttpResponseMessage response = await client.GetAsync(uri);