I have an object that I want to serialize to JSON and use camelcase. I have a .Net Core 3 Web Api application and in my startup file I have the following service registrations:
services.AddControllersWithViews();
services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
});
In my controller I have the following:
var js = JObject.FromObject(obj);
Now, the result of this serialization is something like:
{
StartRow: 1,
EndRow: 2
}
When I instantiate a JsonSerializer, and pass it as the second argument, then I get the expected camelcase format:
{
startRow: 1,
endRow: 2
}
Here is the code:
var serializer = new JsonSerializer()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
var js = JObject.FromObject(obj, serializer);
This would mean I have to have a serializer for each controller.
I'm wondering if I'm missing something or registering something wrongly.
Thank you.