【问题标题】:HttpClient.PostAsJsonAsync can`t Serialize inherited propertyHttpClient.PostAsJsonAsync 无法序列化继承的属性
【发布时间】: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”都是对的。

我用谷歌搜索,但找不到原因。

【问题讨论】:

    标签: httpclient asp.net-web-api


    【解决方案1】:

    改用restsharp,效果很好

    【讨论】:

      猜你喜欢
      • 2012-11-28
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-10
      • 1970-01-01
      相关资源
      最近更新 更多