问题
After migrating my project from Core 2.1. to 2.2. I am having trouble with my Kendo widgets. Fields in the model are specified with PascalCase and the field names returned from the server in the JSON are using camelCase.
I've added DefaultContractResolver in Startup but JSON is still serialized in camelCase. Any workaround here?
services
.AddMvc(options => options.EnableEndpointRouting = false).SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
回答1:
We had a similar problem with Syncfusion expecting PascalCase.
Until now the only solution we found is to create our own
PascalCasePropertyNamesContractResolver : DefaultContractResolver
Therein we just override the ResolvePropertyName to return the key as is.
Unfortunately we have to reference this ContractResolver in each Json-Return, like this:
return Json(new { result = result.Items, count = result.Count }, new JsonSerializerSettings { ContractResolver = new PascalCasePropertyNamesContractResolver () });
If there are better solutions coming up here: welcome and thanks in advance.
来源:https://stackoverflow.com/questions/55416179/change-json-serialization-from-camelcase-to-pascalcase