将嵌套集合发布到 Web API
Posting Nested Collection to Web API
我正在尝试 post 一个复杂类型的对象到网络 api。在 Web api 端,当方法接收对象参数时,每个 属性 都被正确设置,除了从 ICollection 派生的集合。
这是我的示例 类:
public class MyClass
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private MyCollection<string> collection;
public MyCollection<string> Collection
{
get { return collection; }
set { collection = value; }
}
}
public class MyCollection<T> : ICollection<T>
{
public System.Collections.Generic.List<T> list;
public MyCollection()
{
list = new List<T>();
}
public void Add(T item)
{
list.Add(item);
}
public void Clear()
{
list.Clear();
}
public bool Contains(T item)
{
return list.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
list.CopyTo(array, arrayIndex);
}
public int Count
{
get { return list.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(T item)
{
list.Remove(item);
return true;
}
public IEnumerator<T> GetEnumerator()
{
return list.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return list.GetEnumerator();
}
}
这是我的 api 控制器:
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public string Post([FromBody]MyClass value)
{
return "The object has " + value.Collection.Count + " collection item(s).";
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
这是我在客户端的测试方法:
function Test() {
var obj = {
'Id': '15',
'Collection': [{
'': 'item1'
}, {
'': 'item2'
}]
};
$.post(serviceUrl, obj)
.done(function (data) {
alert(data);
});
在 Web 上 Api post 方法 ID 变为 15 但 Collection 的长度为 0。
但是当我将集合类型从 MyCollection 更改为 ICollection 时。集合的长度为 2.
为什么我在使用 MyCollection 时得到零长度集合?是不是执行错了?我怎样才能让它发挥作用?
我认为您需要像这样创建一个模型活页夹:
Post([ModelBinder(typeof(MyClassModelBinder))] MyClass myClass)
如何操作请阅读以下文章:
parameter binding in aspnet web api
我正在尝试 post 一个复杂类型的对象到网络 api。在 Web api 端,当方法接收对象参数时,每个 属性 都被正确设置,除了从 ICollection 派生的集合。
这是我的示例 类:
public class MyClass
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
private MyCollection<string> collection;
public MyCollection<string> Collection
{
get { return collection; }
set { collection = value; }
}
}
public class MyCollection<T> : ICollection<T>
{
public System.Collections.Generic.List<T> list;
public MyCollection()
{
list = new List<T>();
}
public void Add(T item)
{
list.Add(item);
}
public void Clear()
{
list.Clear();
}
public bool Contains(T item)
{
return list.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
list.CopyTo(array, arrayIndex);
}
public int Count
{
get { return list.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(T item)
{
list.Remove(item);
return true;
}
public IEnumerator<T> GetEnumerator()
{
return list.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return list.GetEnumerator();
}
}
这是我的 api 控制器:
public class ValuesController : ApiController
{
// GET api/values
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public string Post([FromBody]MyClass value)
{
return "The object has " + value.Collection.Count + " collection item(s).";
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
这是我在客户端的测试方法:
function Test() {
var obj = {
'Id': '15',
'Collection': [{
'': 'item1'
}, {
'': 'item2'
}]
};
$.post(serviceUrl, obj)
.done(function (data) {
alert(data);
});
在 Web 上 Api post 方法 ID 变为 15 但 Collection 的长度为 0。
但是当我将集合类型从 MyCollection 更改为 ICollection 时。集合的长度为 2.
为什么我在使用 MyCollection 时得到零长度集合?是不是执行错了?我怎样才能让它发挥作用?
我认为您需要像这样创建一个模型活页夹:
Post([ModelBinder(typeof(MyClassModelBinder))] MyClass myClass)
如何操作请阅读以下文章: parameter binding in aspnet web api