-1

Please consider this code:

var ResultMaster = new Master()
{
    Id = Result.Id,
    CityCode = Result.CityCode,
    Desc = Result.Desc,
    EmployeeId = Result.EmployeeId,
    IsDelete = Result.IsDelete,
    LastChange = Result.LastChange,
    StateCode = Result.StateCode,
};

return Ok(ResultMaster);

When I call my Web API method I get camel cased json:

'{"id":1,"cityCode":"001","desc":null,"employeeId":36648,"isDelete":false,"lastChange":"2021-11-25","stateCode":"01"}'

and so I can deserialize it using

System.Text.Json.JsonSerializer.Deserialize<T>

public static async Task<T?> GetObjectFromResponse<T>(this Task<HttpResponseMessage> responseMessage)
{
      var Response = await responseMessage;
      string StringContent = await Response.Content.ReadAsStringAsync();
      var Obj = JsonSerializer.Deserialize<T>(StringContent);
      //var Obj = JsonSerializer.Deserialize<T>("{\"Id\":1,\"CityCode\":\"001\",\"Desc\":null,\"EmployeeId\":36648,\"IsDelete\":false,\"LastChange\":\"2021-11-25\",\"StateCode\":\"01\"}");
      return Obj;
}

I call above method in my Blazor WASM like this:

var Master = await _httpClient.GetAsync($"/api/Ques/GetObject?id={id}").GetObjectFromResponse<MasterDTO>();
return Master;

and I qet empty Master object (not null). But if I change property name to Pascal-case (above commented code) then I get proper object.

How I can set configuration for my API serializer preserve properties name?

Thanks

Arian
  • 12,793
  • 66
  • 176
  • 300
  • Just use Newtonsoft . Json serializer. With Ms serializer your problems are only starting. – Serge Jan 29 '22 at 12:50
  • You compare Newtonsoft and Microsoft? A wheat seed with the sun? – Arian Jan 29 '22 at 15:26
  • This sun will have to buy a wheat seed sooner or later, as it was with many another technologies. Mostly only teenagers are working for MS nowdays. So the quality is getting worster and worster. – Serge Jan 29 '22 at 15:31
  • Wheat seed needs sunlight to bloom – Arian Jan 29 '22 at 15:56

1 Answers1

-1

just add this code in ConfigureServices section:

services.AddControllers()
        .AddJsonOptions(options => 
        {
            options.JsonSerializerOptions.PropertyNamingPolicy = null;
        });
Arian
  • 12,793
  • 66
  • 176
  • 300