它位于.net核心WebAPI中,它不会在以下代码中读取$ type
// POST api/values
[HttpPost("Order")]
public string Order([FromBody] OrderRequest rq)
{
var a = new OrderRequest
{
Version = "12",
Typea = new MsgType
{
A = new MsgTypeA
{
Item = new MsgSubTypeA
{
SubA = "TEST"
}
}
}
};
return JsonConvert.SerializeObject(a, Formatting.Indented, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto//.All
});
}
请求
{
"Typea": {
"A": {"Item": {
"$type": "Services.WebReference.MsgSubTypeA, Services.WebReference",
"SubA":"TEST"
}
}}
}
}
它只是无法读取$ type,并且无法将“Item”转换为正确的类型,而是转换为其基类。但是,每个JsonConvert.SerializeObject生成的响应显示相同的JSON。
怎么了?
答案 0 :(得分:2)
现在已经解决了
public string Order([FromBody] JObject rq)
{
var obj = JsonConvert.DeserializeObject<OrderRequest>(rq.ToString(), new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto
});
另一种方式可能更好:
services
.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto;
})