传递对象的数组的WebAPI [英] pass array of an object to webapi
问题描述
我有我想要一个对象的数组传递给方法,我的控制器上的.NET MVC 4的WebAPI项目。
I have a .net mvc 4 webapi project that I'm trying to pass an array of an object to a method on my controller.
我已经找到一些例子在这里,这样说说需要与我的设置对象的属性:参数1 =无论&放大器;参数2 =金光闪闪&放大器;参数3 =等等
I've found some examples here on SO that talk about needing to set my object's properties with: param1=whatever¶m2=bling¶m3=blah.
但我不明白我怎么能在集合中使用过。
But I don't see how I can pass in a collection using that.
下面是我的方法签名。请注意,我已经布置参数与[FromUri]属性。
Here is my method signature. Notice I've decorated the argument with the [FromUri] attribute.
public List<PhoneResult> GetPhoneNumbersByNumbers([FromUri] PhoneRequest[] id)
{
List<PhoneResult> prs = new List<PhoneResult>();
foreach (PhoneRequest pr in id)
{
prs.Add(PhoneNumberBL.GetSinglePhoneResult(pr.PhoneNumber, pr.RfiDate, pr.FinDate, pr.State));
}
return prs;
}
这里是我的简单PhoneRequest对象:
here is my simple PhoneRequest object:
public class PhoneRequest
{
public string PhoneNumber { get; set; }
public string RfiDate { get; set; }
public string FinDate { get; set; }
public string State { get; set; }
}
和这里是我使用在传递什么的示例:
and here's a sample of what I'm using to pass in:
http://localhost:3610/api/phonenumber/getphonenumbersbynumbers/
[{"PhoneNumber":"8016667777","RfiDate":"","FinDate":"2012-02-11","State":"UT"},
{"PhoneNumber":"8018889999","RfiDate":"2012-12-01","FinDate":"","State":"UT"}]
使用
这回来了错误的请求
using this comes back with "bad request"
我也试过这种
http://localhost:3610/api/phonenumber/getphonenumbersbynumbers?
id=[{"PhoneNumber":"8016667777","RfiDate":"","FinDate":"2012-02-11","State":"UT"},
{"PhoneNumber":"8018889999","RfiDate":"2012-12-01","FinDate":"","State":"UT"}]
这确实达到了方法,但该数组为null。
which does reach the method, but the array is null.
如何在我的PhoneRequest对象的数组传递给我的WebAPI的方法?
how can I pass in an array of my PhoneRequest object to my WebAPI method?
TIA
推荐答案
尝试传递PhoneRequest []从按该格式的URI:
Try passing the PhoneRequest[] from the uri in this format:
http://localhost:3610/api/phonenumber/getphonenumbersbynumbers?
id[0][PhoneNumber]=8016667777&id[0][FinDate]=2012-02-11&id[0][State]=UT&
id[1][PhoneNumber]=8018889999&id[1][RfiDate]=2012-12-01&id[1][State]=UT
这篇关于传递对象的数组的WebAPI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!