2

We have an asp.net-core5 endpoint who return an object

   public class ServiceResponse
        {
            public Balance Balance { get; set; }
            public int Code { get; set; }
            public object Message { get; set; }
        }

        public class Balance
        {
            public float Amount { get; set; }
            public string Currency { get; set; }
        }

But for some reason the response we received is

{
    "$id": "1",
    "balance": {
        "$id": "2",
        "amount": 2130.804,
        "currency": "USD"
    },
    "code": 0,
    "message": null
}

Notice the response adds :""$id": "1"" and changes the properties to lowercase

The methods is an async Task

 public async Task<IActionResult> GetBalance([FromBody] cl_Balance value)

and we return

 return Ok(ServiceResponse);

Hugo Leiva
  • 31
  • 4

1 Answers1

1

The solution was to change:

services.AddControllers().AddJsonOptions(x => x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);

to

 services.AddControllers().AddJsonOptions(x => x.JsonSerializerOptions.PropertyNamingPolicy=null);
Hugo Leiva
  • 31
  • 4