Raymond
Raymond

Reputation: 71

HttpClient.PostAsJsonAsync can`t Serialize inherited property

my code is

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; }
}

I call web-api by httpclient

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)
        {

        }
    }

when i debug,i found the data that server received ,"Id","Code" and "Name" inherited from base class were all null,"Level" and "ChangedDate" were right.

I googled,but I cannot find my reason.

Upvotes: 3

Views: 2311

Answers (1)

Raymond
Raymond

Reputation: 71

changed to use restsharp,it works well

Upvotes: 1

Related Questions