3

我的代码是

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”是正确的。

我用谷歌搜索,但我找不到我的理由。

4

1 回答 1

1

改用restsharp,效果很好

于 2012-12-14T00:44:21.977 回答