Note on migrating Web API controllers from ASP.NET 4.x to ASP.NET Core: JSON object casing changes by default
Posted: (EET/GMT+2)
When migrating Web API controllers from ASP.NET 4.x to ASP.NET Core, you may notice that JSON property casing changes by default. ASP.NET 4.x used PascalCase for serialized object properties; ASP.NET Core's default JSON serializer (based on Newtonsoft.Json) switched to camelCase.
Here's an example of how the results look like:
// ASP.NET 4.x
public class Product {
public string ProductName { get; set; }
}
// JSON output:
{ "ProductName": "Keyboard" }
// ASP.NET Core default
{ "productName": "Keyboard" }
This can surprise client applications expecting the old casing. To restore the previous behavior, configure JSON options in Startup.cs:
services.AddMvc()
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new DefaultContractResolver();
});
The above setting restores PascalCase naming. For new APIs, using camelCase is generally preferred because it matches common JavaScript conventions, but for migrated clients, explicit configuration avoids breaking changes.