Search code examples
c#.netasp.net-corejson.netcamelcasing

How to stop newtonsoft json from camelcasing


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.


Solution

  • In your startup's ConfigureServices you can add MvcJsonOptions.

    services
        .AddMvc()
        .AddJsonOptions(options =>
        {
            options.SerializerSettings.ContractResolver =
                new Newtonsoft.Json.Serialization.DefaultContractResolver();
        });