WebAPI 2.0 Post Not Deserializing List<T> Property

冷暖自知 提交于 2019-12-23 16:23:51

问题


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).

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).

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

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.

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

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.

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


回答1:


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>).



来源:https://stackoverflow.com/questions/43008864/webapi-2-0-post-not-deserializing-listt-property

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