Creating a new Web API ASP.NET Core 3 project and removing the call to .AddNewtonsoftJson(), then changing the Get() method on the controller to return an object with properties causes it to return JSON, but with all of the property names converted to camel case.
The only option for SerializerOptions.PropertyNamingPolicy is JsonNamingPolicy.CamelCase; there is no option to not modify the case at all.
Steps to reproduce the behavior:
.AddNewtonsoftJson(). public TestModel(string value)
{
MultiWordValue = value;
}
}
```
Get() method body to return new TestModel("value");GET api/values returns {"multiWordValue":"value"}I expect it to not change the casing on the property names unless I explicitly ask for that behaviour; barring that, I expect to have a way to tell it to preserve casing: {"MultiWordValue":"value"}
.NET Core SDK (reflecting any global.json):
Version: 3.0.100-preview5-011568
Commit: b487ff10aa
Runtime Environment:
OS Name: Windows
OS Version: 10.0.17763
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnetsdk\3.0.100-preview5-011568\
Host (useful for support):
Version: 3.0.0-preview5-27626-15
Commit: 61f30f5a23
.NET Core SDKs installed:
2.1.202 [C:\Program Files\dotnetsdk]
2.1.504 [C:\Program Files\dotnetsdk]
2.1.505 [C:\Program Files\dotnetsdk]
2.1.600 [C:\Program Files\dotnetsdk]
2.1.601 [C:\Program Files\dotnetsdk]
2.1.602 [C:\Program Files\dotnetsdk]
2.1.700-preview-009597 [C:\Program Files\dotnetsdk]
2.1.700-preview-009601 [C:\Program Files\dotnetsdk]
2.1.700-preview-009618 [C:\Program Files\dotnetsdk]
2.2.104 [C:\Program Files\dotnetsdk]
3.0.100-preview5-011568 [C:\Program Files\dotnetsdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.8 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.2 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.8 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.2 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0-preview5-19227-01 [C:\Program Files\dotnetshared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnetshared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.8 [C:\Program Files\dotnetshared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnetshared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.2 [C:\Program Files\dotnetshared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0-preview5-27626-15 [C:\Program Files\dotnetshared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0-preview5-27626-15 [C:\Program Files\dotnetshared\Microsoft.WindowsDesktop.App]
Using [JsonPropertyName("MultiWordValue")] on the property does have it use the specified casing, but having to put that Attribute, redundantly, on all properties can be a lot of work that is prone to copy and paste errors/typos.
MVC expects camel-case JSON input and will camel-case all JSON output by default. To use property's original casing, you can set the PropertyNamingPolicy to null.
/cc @steveharter \ @ahsonkhan
Most helpful comment
MVC expects camel-case JSON input and will camel-case all JSON output by default. To use property's original casing, you can set the
PropertyNamingPolicytonull./cc @steveharter \ @ahsonkhan