WebAPI 2.0发布未反序列化列表< T>财产 [英] WebAPI 2.0 Post Not Deserializing List<T> Property

查看:56
本文介绍了WebAPI 2.0发布未反序列化列表< T>财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经查看了我在这里可以找到的每一篇类似的文章,并尝试了所有可能无法成功进行的尝试(因为我现在提的是问题,所以不推荐).

I have reviewed every similar article I can find here and tried anything that looked like it might work with no success (obv since I am now asking the question).

我有一个带POST操作的webAPI 2.0控制器,该操作接受类型为Reservation的对象.除其他事项外,该对象包含一个名为Items的属性,该属性的类型为EquipmentItems.可以想象,这是一个List属性.我将保留对象发送过来(如果对任何人都重要,请使用PostAsJsonAsync("api/Reservation", reservation).Result).

I have a webAPI 2.0 controller with a POST action that takes in an object of type Reservation. This object contains, among other things, a property called Items which is of type EquipmentItems. As you can imagine, this is a List property. I send the reservation object over (using PostAsJsonAsync("api/Reservation", reservation).Result if it matters to anyone).

当我进入API控制器时,除了EquipmentItems属性中的所有内容外,Reservation对象完全填充了所有内容.

When I land in the API controller, the Reservation object is completely populated with everything except what is in the EquipmentItemsproperty.

本着完全公开的精神,Reservation类中的Items属性实际上定义为List,其中T是IItemData接口. EquimpentItem是从IItemData继承而来的,因此不确定是否会使事情复杂化.

In the spirit of full disclosure, the Items property within the Reservation class, is actually defined as a List where T is IItemData interface. EquimpentItem inherits from IItemData tho, so not sure if that complicates matters.

本机控制器反序列化器是否不能处理T为接口的List?

Can the native controller deserializer not handle List where T is interface?

我知道的工作是将列表定义为常规数组.效果很好,但是我还有其他要求引导我使用List.

What I know does work is defining the List as a regular array. That works very well, but I have other requirements that have navigated me towards using List.

关于如何正确反序列化List属性的任何建议?

Any suggestions on how to get that List property deserialized correctly?

推荐答案

正确.如果您尝试反序列化为List<ISomething>,则它将不起作用.原因是反序列化操作不知道您要实际创建哪个ISomething(您不能初始化ISomething,因为它不是具体的类).您将需要更改服务接口以显示该列表的具体类. (即List<Something>).

Correct. If you are trying to deserialize into a List<ISomething>, then it will not work. The reason is the deserialization operation does not know which ISomething you wish to actually create (you cannot init an ISomething, because its not a concrete class). You will need to change the service interface to expose a concrete class for the list. (ie. List<Something>).

这篇关于WebAPI 2.0发布未反序列化列表&lt; T&gt;财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆