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
just add this code in ConfigureServices
section:
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.