出于某种原因,我似乎无法将一组值传递给我的 Web API 方法。这是我的代码:
[HttpGet("api/values/get/{ids}")]
public JsonResult GetValues(string[] valueIds)
{
//Do something
}
我试过通过以下 URL 调用它:
- http://localhost:5001/api/values/get/?valueIds=das&valueIds=ewq
- http://localhost:5001/api/values/get/?ids=das&ids=ewq
- http://localhost:5001/api/values/get?valueIds=das&valueIds=ewq
- http://localhost:5001/api/values/get?ids=das&ids=ewq
当我通过以下 URL 调用它时,我没有得到 404 并且实际上调用了该方法,但是,该数组是空的:
- http://localhost:5001/api/values/get/valueIds=das&valueIds=ewq
- http://localhost:5001/api/values/get/ids=das&ids=ewq
我还尝试将属性 [FromQuery] 添加到方法签名中的参数前面,如下所示:
public JsonResult GetValues([FromQuery]string[] valueIds)
我似乎无法让它工作,即使那里的每个教程都说它应该工作。在这里撕扯我的头发。
请您参考如下方法:
删除 {ids} 前缀,因为操作选择器需要 ids 参数,但您没有在操作中传递它。
[HttpGet("api/values/get")]
public JsonResult GetValues(string[] valueIds)
{
//Do something
}
然后像这样应用请求;
http://localhost:5001/api/values/get?valueIds=das&valueIds=ewq
