请考虑此代码:
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);
当我调用我的Web API方法时,我得到了骆驼大小写的json:
'{"id":1,"cityCode":"001","desc":null,"employeeId":36648,"isDelete":false,"lastChange":"2021-11-25","stateCode":"01"}'
因此我可以使用对其进行反序列化
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;
}
我在Blazor WASM中调用上面的方法,如下所示:
var Master = await _httpClient.GetAsync($"/api/Ques/GetObject?id={id}").GetObjectFromResponse<MasterDTO>();
return Master;
并且Iqet为空CCD_ 1对象(非空(。但如果我将属性名称更改为Pascal大小写(上面注释的代码(,那么我就得到了合适的对象。
如何设置API序列化程序的配置以保留属性名称?
感谢
只需在ConfigureServices
部分添加此代码:
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = null;
});