Search code examples
c#asp.net-web-api2xml-deserialization

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

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?


Solution

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