I am using the NewtonSoft json serializer in an ASP.NET Core 8 WebApi.
In my program.cs I have configured like this.
builder.Services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowReadingFromString;
options.JsonSerializerOptions.PropertyNamingPolicy = null;
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
})
.AddNewtonsoftJson(options =>
{
});
I want the serialized properties to retain the case of the model, so if I have
[HttpPost, Route("api/error")]
public async Task<dynamic> Error()
{
return new { Error = "Bad" };
}
This serializes as
{ error: "Bad" }
The error has lost the upper case E. How do I stop that?
I just defined ContractResolver
on SerializerSettings
with NamingStrategy = new DefaultNamingStrategy()
as below:
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver
{
NamingStrategy = new DefaultNamingStrategy()
};
});
and it worked 🙂