HttpClient.PostAsJsonAsync 无法序列化继承的属性
Posted
技术标签:
【中文标题】HttpClient.PostAsJsonAsync 无法序列化继承的属性【英文标题】:HttpClient.PostAsJsonAsync can`t Serialize inherited property 【发布时间】:2012-08-16 12:49:48 【问题描述】:我的代码是
public class BaseDTO
public int Id get; set;
public string Code get; set;
public string Name get; set;
public class DataDTO : BaseDTO
public int Level get; set;
public DateTime ChangedDate get; set;
我通过httpclient调用web-api
static void Main(string[] args)
var httpClientHandler = new HttpClientHandler();
httpClientHandler.UseDefaultCredentials = true;
var client = new HttpClient(httpClientHandler);
client.BaseAddress = new Uri("http://localhost/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var dto = new DataDTO()
Id = 1,
Code = "a",
Name = "A",
Level = 10,
ChangedDate = DateTime.Now
;
HttpResponseMessage resp =
client.PostAsJsonAsync(
"api/MyApi/Creat", dto).Result;
if (resp.IsSuccessStatusCode)
我在调试的时候发现服务器接收到的数据,“Id”,“Code”和“Name”继承自基类都是空的,“Level”和“ChangedDate”都是对的。
我用谷歌搜索,但找不到原因。
【问题讨论】:
【参考方案1】:改用restsharp,效果很好
【讨论】:
以上是关于HttpClient.PostAsJsonAsync 无法序列化继承的属性的主要内容,如果未能解决你的问题,请参考以下文章