gpt4 book ai didi

c# - ApiController 操作无法从查询字符串中解析数组

转载 作者:行者123 更新时间:2023-11-30 14:58:36 25 4
gpt4 key购买 nike

使用 Visual Studio 2012.2,MVC4 网络应用程序。

我的 ApiController 有这样的请求:

http://localhost/api/keys?ids[]=1&ids[]=2&ids[]=3

我的印象是以下方法应该能够自动从 ids[] 数组中检索值:

public KeysModel Get(int[] ids){...}

但是,当像上面这样的请求进来时,“ids”参数的值为null。

我检查过 HttpContext.Current.Request.QueryString 具有 id 的值,我可以通过这种方式获取它们,但这会使单元测试更加困难。

我也尝试过使用 List ids、[FromUri]、[FromUri(Name="ids[]")]、对象 ids 和字符串 ids(有趣的是……当 ids 是一个字符串变量时,其中的值它是“(集合)”

最佳答案

结果:

public KeysModel Get([FromUri]int[] ids){...}

毕竟是答案。

不知道我之前在做什么......

关于c# - ApiController 操作无法从查询字符串中解析数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18539057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com