我可以将一些虚假/测试数据传递给Web API方法,如下所示:
string dataAsJson = "[{\"ItemDescription\": \"DUCKBILLS, GRAMPS-EIER 70CT 42#\",\"PackagesMonth1\": 1467}]";
String uriToCall = String.Format("/api/produceusage/{0}/{1}/{2}", _unit, beginRange, endRange);
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("", dataAsJson)
});
HttpResponseMessage response = await client.PostAsync(uriToCall, content);
测试数据在服务器上接收就好了。
但是,我需要传递比KeyValuePair的简单数组更复杂的数据 - 一个自定义类的通用列表。在下面的代码中,_dtDelPerf是一个List
string dataAsJson = JsonConvert.SerializeObject(_dtDelPerf);
String uriToCall = String.Format("/api/deliveryperformance/{0}/{1}/{2}", _unit, _beginDateYYYYMMDD, _endDateYYYYMMDD);
var content = new FormUrlEncodedContent(new[]
{
//new List<DeliveryPerformance>("", dataAsJson)
new List<DeliveryPerformance>(dataAsJson)
});
HttpResponseMessage response = await client.PostAsync(uriToCall, content);
注释掉的尝试(“new List(”“,dataAsJson)”)和它下面的尝试都不起作用。第一个,我得到了几个编译错误,例如,“'System.Collections.Generic.List'不包含带有2个参数的构造函数'”
第二个,在删除第一个arg后,它是:
new List<DeliveryPerformanceClass>(dataAsJson)
...我得到其他编译错误,例如“ Argument 1:无法从'string'转换为'int'”
我需要做些什么来推动这些数据?