gpt4 book ai didi

HttpClient.PostAsJsonAsync 无法序列化继承的属性

转载 作者:行者123 更新时间:2023-12-05 06:47:15 24 4
gpt4 key购买 nike

我的代码是

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

}
}

调试的时候发现server收到的数据,从基类继承的“Id”,“Code”和“Name”都是null,“Level”和“ChangedDate”是对的。

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

最佳答案

改为使用restsharp,效果很好

关于HttpClient.PostAsJsonAsync 无法序列化继承的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11987507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com