I am implementing a web API 2 service that uses JSON.NET for serialization.
When I try to update the json data of PUT (deseralize), the abstract class is missing because it did not know what to do with it, therefore it did nothing. I also tried to make the NOT class abstract and simply inherit from it, and then each PUT was processed in the base class, and not in the derrive class, which does not have the properties of the derrive class.
Example:
public class People { // other attributes removed for demonstration simplicity public List<Person> People { get;set; } } public abstract class Person { public string Id {get;set;} public string Name {get;set;} } public class Employee : Person { public string Badge {get;set;} } public class Customer : Person { public string VendorCategory {get;set;} }
with my web api configured to handle type names:
public static void Register(HttpConfiguration config) { config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling = TypeNameHandling.Objects; }
then i put json like:
{ people: [{ name: "Larry", id: "123", badge: "12345", $type: "API.Models.Employee, API" }] }
for the web api method:
public HttpResponseMessage Put(string id, [FromBody]People value) { people.Update(value);
but the output when checking value always:
People == { People: [] }
or if not abstract:
People == { People: [{ Name: "Larry", Id: "123" }] }
missing inheritited property. Has anyone encountered this problem and came up with something?
amcdnl Dec 05 '13 at 19:24 2013-12-05 19:24
source share