记录为空的响应获取

huangapple go评论65阅读模式
英文:

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&lt;CustomerItsTaskDetailDto&gt; 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(&quot;its/customerdetail&quot;)]
    public async Task&lt;CustomerItsDetailDto&gt; GetItsCustomerDetailInPmaktif(string customerCode) =&gt; 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&lt;CustomerItsTaskDetailDto&gt; TaskDetails;
    }

Credits to DavidG for his excellent comment (see above).
You should declare your record as follows:

public record CustomerItsDetailDto(string VirtualVisitStatus, List&lt;CustomerItsTaskDetailDto&gt; TaskDetails);

this will create {get; init;} properties.

Or you can create the properties yourself.

public record CustomerItsDetailDto
{ 
    public string VirtualVisitStatus {get; init;}
    public List&lt;CustomerItsTaskDetailDto&gt; TaskDetails {get; init;} = new()
}

huangapple
  • 本文由 发表于 2023年7月13日 16:19:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定