Search code examples
c#responserecord

Record as Response Getting Empty


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?


Solution

  • 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()
    }