英文:
Record as Response Getting Empty
问题
我有一个名为CustomerItsDetailDto的记录类型的响应Dto,如下所示:
public record CustomerItsDetailDto
{
public string VirtualVisitStatus;
public List<CustomerItsTaskDetailDto> TaskDetails;
}
当我调试时,我可以看到响应不是空的。但是在Postman中我收到了空响应。问题出在哪里?
控制器部分:
控制器看起来像这样;
[HttpGet]
[Route("its/customerdetail")]
public async Task<CustomerItsDetailDto> GetItsCustomerDetailInPmaktif(string customerCode) => await posRepository.GetItsCustomerDetail(customerCode);
我知道如果我更改记录类,我可以获得响应。但是为什么记录类型不起作用呢?
英文:
I have Response Dto as record type like below:
public record CustomerItsDetailDto
{
public string VirtualVisitStatus;
public List<CustomerItsTaskDetailDto> TaskDetails;
}
When I debug it I can see the response is not empty. But I am getting empty response in the postman. What is the problem here?
Controller part;
Controller seems like this;
[HttpGet]
[Route("its/customerdetail")]
public async Task<CustomerItsDetailDto> GetItsCustomerDetailInPmaktif(string customerCode) => await posRepository.GetItsCustomerDetail(customerCode);
I know that if I change record the class i can get the response. But why record is not working?
答案1
得分: 0
你正在声明字段而不是属性。
public record CustomerItsDetailDto
{
public string VirtualVisitStatus;
public List<CustomerItsTaskDetailDto> TaskDetails;
}
Credits to DavidG for his excellent comment (see above).
你应该将你的记录声明如下:
public record CustomerItsDetailDto(string VirtualVisitStatus, List<CustomerItsTaskDetailDto> TaskDetails);
这将创建 {get; init;}
属性。
或者你也可以自己创建属性。
public record CustomerItsDetailDto
{
public string VirtualVisitStatus { get; init; }
public List<CustomerItsTaskDetailDto> TaskDetails { get; init; } = new List<CustomerItsTaskDetailDto>();
}
英文:
You are declaring fields instead of properties.
public record CustomerItsDetailDto
{
public string VirtualVisitStatus;
public List<CustomerItsTaskDetailDto> TaskDetails;
}
Credits to DavidG for his excellent comment (see above).
You should declare your record as follows:
public record CustomerItsDetailDto(string VirtualVisitStatus, List<CustomerItsTaskDetailDto> TaskDetails);
this will create {get; init;}
properties.
Or you can create the properties yourself.
public record CustomerItsDetailDto
{
public string VirtualVisitStatus {get; init;}
public List<CustomerItsTaskDetailDto> TaskDetails {get; init;} = new()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论