将数组传递给asp net core web api action方法HttpGet [英] passing an array to a asp net core web api action method HttpGet
问题描述
我正在尝试向我的操作方法发送一个整数数组,代码如下所示:
I am trying to send an array of integers to my action method the code looks like so:
[HttpGet]
public async Task<IActionResult> ServicesByCategoryIds([FromQuery] int[] ids)
{
var services = await _accountsUow.GetServiceProfilesByCategoryIdsAsync(ids);
return Ok(services);
}
我这样调用方法:https://localhost:44343/api/accounts/servicesbycategoryids?ids=1&ids=2
但是当我调用这个方法时总是得到一个空数组,即使我在查询字符串中传递了 id.我正在使用 .net 核心 2.1.
but always get en empty array when I call this method even tho I pass the ids in the query string. I am using .net core 2.1.
我在谷歌上搜索的所有内容都表明这实际上就是这样做的方式...有什么我在这里遗漏的吗?
everything I have googled suggests that this is in fact the way this is done. . . is there something I am missing here?
谢谢!
推荐答案
Binding failed for Array
parameter is a known issue under Asp.Net Core 2.1
已记录查询字符串中的数组或列表未解析#7712.
Binding failed for Array
parameter is a known issue under Asp.Net Core 2.1
which has been recorded Array or List in query string does not get parsed #7712.
对于临时解决方法,您可以像下面这样设置 FromQuery Name Property
:
For a tempory workaround, you could set the FromQuery Name Property
like below:
[HttpGet()]
[Route("ServicesByCategoryIds")]
public async Task<IActionResult> ServicesByCategoryIds([FromQuery(Name = "ids")]int[] ids)
{
return Ok();
}
这篇关于将数组传递给asp net core web api action方法HttpGet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!