【问题标题】:Passing parameters to via GetAsync()通过 GetAsync() 传递参数
【发布时间】:2018-12-03 17:11:49
【问题描述】:

我有一个想要测试的 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 获取到该方法?

【问题讨论】:

  • 使用查询字符串它是一个get ?param=foo&amp;param2=bar

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


【解决方案1】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2022-01-25
  • 2014-05-22
  • 1970-01-01
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
相关资源
最近更新 更多