Posting Nested Collection to Web API

隐身守侯 提交于 2020-01-03 17:38:46

问题


I'm trying to post a complex type object to web api. On the web api side, when method recieves the object parameter, every property is set properly except a collection that is derived from ICollection.

Here is my sample classes:

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();
    }
}

Here is my api controller:

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)
    {
    }
}

Here is my test method on client side:

        function Test() {
        var obj = {
            'Id': '15',
            'Collection': [{
                '': 'item1'
            }, {
                '': 'item2'
            }]
        };
        $.post(serviceUrl, obj)
        .done(function (data) {
            alert(data);
        });

On Web Api post method Id becomes 15 but Collection's length is 0.

But when I change collection type to ICollection from MyCollection. Collection's length is 2.

Why am I getting zero-length collection when I use MyCollection? Is it implemented wrong? How can I make it work?


回答1:


I think you need to create a model binder like this:

Post([ModelBinder(typeof(MyClassModelBinder))] MyClass myClass)

How to do it please read the following article: parameter binding in aspnet web api



来源:https://stackoverflow.com/questions/31580396/posting-nested-collection-to-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!