将嵌套集合发布到Web API

编程入门 行业动态 更新时间:2024-10-25 10:26:42
本文介绍了将嵌套集合发布到Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将复杂类型的对象发布到Web API.在网络api端,当方法接收到对象参数时,除从ICollection派生的集合外,每个属性均已正确设置.

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.

这是我的示例课程:

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控制器:

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

在Web Api上发布方法ID变为15,但Collection的长度为0.

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

但是当我将集合类型从MyCollection更改为ICollection时.集合的长度为2.

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

为什么使用MyCollection时会出现零长度集合?实施错误吗?我该如何运作?

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

推荐答案

我认为您需要创建一个这样的模型装订器:

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

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

如何执行此操作,请阅读以下文章: 参数绑定在ASPNET Web API

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

更多推荐

将嵌套集合发布到Web API

本文发布于:2023-10-12 01:22:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483357.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   Web   API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!