1

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());

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794

1 Answers1

3

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.

  • "Unfortunately we have to reference this ContractResolver in each Json-Return, like this" This may work: `JsonConvert.DefaultSettings = new JsonSerializerSettings { ContractResolver = new PascalCasePropertyNamesContractResolver () }` (From [this answer](https://stackoverflow.com/a/21815974/569302)) – Jesus is Lord Dec 29 '21 at 16:53
  • Here's [code](https://stackoverflow.com/a/70522764/569302) based on this answer – Jesus is Lord Dec 29 '21 at 17:18