WebAPI 2.0发布未反序列化列表< T>财产 [英] WebAPI 2.0 Post Not Deserializing List<T> Property
问题描述
我已经查看了我在这里可以找到的每一篇类似的文章,并尝试了所有可能无法成功进行的尝试(因为我现在提的是问题,所以不推荐).
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 EquipmentItems
property.
本着完全公开的精神,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发布未反序列化列表< T>财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!