我有一条linq语句,它似乎工作得很好,并且获得了正确的数据:
[HttpGet]
public async Task<IActionResult> Get()
{
List<DiaryRecord> diaryRecords = await this.Context.DiaryRecords
.Include(d => d.Project)
.Include(e => e.Employees)
.ToListAsync();
return Ok(diaryRecords);
}员工是
public virtual ICollection<Personell> Employees { get; set; }我是通过以下方式在客户程序集中请求此列表:
this.DiaryRecords = await this.HttpClient
.GetFromJsonAsync<IEnumerable<DiaryRecordModelDTO>>("api/Diary");雇员在下列地方:
public ICollection<PersonellDTO> Employees { get; set; }作为输出,this.DiaryRecords拥有所有所需的信息,除了这里的员工为null。是由于PersonellDTO和Personell类不同而出现的错误。怎么让它起作用?
发布于 2021-12-26 21:40:30
.GetFromJsonAsync总是非常棘手,从不使用接口来反序列化json。json不知道使用PersonellDTO或Personell创建http响应的类是什么。
在您的DTO接口中,应该将其修复到一个具体的类中。
public List<PersonellDTO> Employees { get; set; }对于httpclient,我总是使用这样的代码
var response = await client.GetAsync(api);
if (response.IsSuccessStatusCode)
{
var stringData = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<List<DiaryRecordModelDTO>(stringData);
}https://stackoverflow.com/questions/70489453
复制相似问题