我正在实现一个使用JSON.NET进行序列化的WebAPI2服务。
当我尝试放入(反序列化)更新的json数据时,抽象类不存在,这意味着它不知道如何处理它,所以它什么也不做。我还试着让类不是抽象的,只是继承它,然后每个PUT都应该放到基类,而不是derrived类,缺少derrived类的属性。
示例:
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;}
}
将我的web api配置为执行typename处理:
public static void Register(HttpConfiguration config)
{
config.Formatters.JsonFormatter.SerializerSettings.TypeNameHandling =
TypeNameHandling.Objects;
}
然后我把JSON放在下面:
{
people: [{
name: "Larry",
id: "123",
badge: "12345",
$type: "API.Models.Employee, API"
}]
}
对web api方法执行以下操作:
public HttpResponseMessage Put(string id, [FromBody]People value)
{
people.Update(value); // MongoDB Repository method ( not important here )
return Request.CreateResponse(HttpStatusCode.OK);
}
但在检查value
时,输出始终是:
People == { People: [] }
或者如果是非抽象的:
People == { People: [{ Name: "Larry", Id: "123" }] }
缺少继承的属性。有没有人遇到这个问题,想出了什么办法?
https://stackoverflow.com/questions/20408771
复制相似问题