Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要向我的Web API发送GET请求中的集合_C#_Asp.net_Asp.net Web Api - Fatal编程技术网

C# 需要向我的Web API发送GET请求中的集合

C# 需要向我的Web API发送GET请求中的集合,c#,asp.net,asp.net-web-api,C#,Asp.net,Asp.net Web Api,我有一个场景,需要在GET请求中将多个ProductCodes发送到我的Web API。在我的asp.net web api控制器中,有没有一种方法可以使我的操作方法的参数的类型为列表ProductCodes。我假设没有,我必须这样传递它们?ProductCodes=a,b,c,然后将其作为ProductCodes字符串的列表接受。如果有更好的办法,请告诉我 我在代码端得到null [HttpGet] [ActionName("product-cache-deletion")] public

我有一个场景,需要在GET请求中将多个ProductCodes发送到我的Web API。在我的asp.net web api控制器中,有没有一种方法可以使我的操作方法的参数的类型为
列表ProductCodes
。我假设没有,我必须这样传递它们
?ProductCodes=a,b,c
,然后将其作为
ProductCodes
字符串的列表接受。如果有更好的办法,请告诉我

我在代码端得到null

[HttpGet] 
[ActionName("product-cache-deletion")] 
public HttpResponseMessage ProductCacheDeletion(List<string> ProductCodeList) { 
    //...
}
但是
ProductCodeList

您可以像下面这样尝试,在其中您可以传递多个值以及 多个参数容易。希望它能帮助你


在请求中使用具有一个名称ProductCodeList的多个参数


在操作中,将[FromURI]属性设置为:

public HttpResponseMessage ProductCacheDeletion([FromUri] IEnumerable<string> ProductCodeList)

当我完成这项工作时,我将参数设置为我所传递内容的单一版本,因此URL的读取更加直观。

您可以使用[FromUri]属性,强制api将查询字符串作为列表读取

[HttpGet] 
[ActionName("product-cache-deletion")] 
public HttpResponseMessage ProductCacheDeletion([FromUri]List<string> ProductCodeList) 
{ 
    //...
}

阅读有关属性的详细信息

[HttpGet][ActionName(“产品缓存删除”)]公共HttpResponseMessage ProductCacheDelete(List ProductCodeList){}这是我试图调用的api方法,但ProductCodeList的everytime值将为null如果重复查询字符串参数,浏览器将它们解释为数组,可能web api也会这样做。试试
?ProductCodes=a&ProductCodes=b
,等等。我投了赞成票,因为我已经测试过并且有效,但是你能提供文档或rfc吗?我可以找到这是“官方”方式吗?
public HttpResponseMessage ProductCacheDeletion([FromUri] IEnumerable<string> ProductCodeList)
/product-cache-deletion?ProductCodeList=a&ProductCodeList=b&ProductCodeList=c
[HttpGet] 
[ActionName("product-cache-deletion")] 
public HttpResponseMessage ProductCacheDeletion([FromUri]List<string> ProductCodeList) 
{ 
    //...
}
http://localhost:59956/api/manuals/product-cache-deletion?ProductCodeList=a&ProductCodeList=b&ProductCodeList=c