0

Configuration:

  • Azure Web API, C#, ASP.NET CORE 2.2
  • Windows Client, C#, .NET Framework 4.7.2
  • Newtonsoft JSON for serializing objects between Server and Client

Problem:

When sending a DataTable, Newtonsoft JSON is always changing the column captions to camelCase.

For example, a column called CostObjectKey is shown as costObjectKey after the transmission.

How can I stop Newtonsoft from doing this? I want my column captions unchanged.

askolotl
  • 964
  • 1
  • 13
  • 27
  • 2
    You can refer for solutions on this: https://stackoverflow.com/questions/23504944/camel-casing-issue-with-web-api-using-json-net – Angelo Nov 06 '19 at 14:34

2 Answers2

2

In your startup's ConfigureServices you can add MvcJsonOptions.

services
    .AddMvc()
    .AddJsonOptions(options =>
    {
        options.SerializerSettings.ContractResolver =
            new Newtonsoft.Json.Serialization.DefaultContractResolver();
    });
davidmdem
  • 3,763
  • 2
  • 30
  • 33
2

The default is now camelCase. If you need/want all of the JSON output to be in PascalCase, then the solution is pretty simple.

All you need to do is specify the DefaultContractResolver. But this not work in asp net core 3.

    public void ConfigureServices(IServiceCollection services)
    {
    // Add framework services.
    services.AddMvc()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver());
    }
  • 1
    Thanks! Just to be precise, I don't want PascalCase either, I want that names are transmitted without any change. – askolotl Nov 06 '19 at 16:50