I'm trying to set the global serializer settings like this in my global.asax
.
var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter; formatter.SerializerSettings = new JsonSerializerSettings { Formatting = Formatting.Indented, TypeNameHandling = TypeNameHandling.Objects, ContractResolver = new CamelCasePropertyNamesContractResolver() };
When serializing object using the following code the global serializer settings are not used?
return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(page)) };
Isn't it possible to set the global serializer settings like this or am I missing something?
Setting the JsonConvert.DefaultSettings
did the trick.
JsonConvert.DefaultSettings = () => new JsonSerializerSettings { Formatting = Formatting.Indented, TypeNameHandling = TypeNameHandling.Objects, ContractResolver = new CamelCasePropertyNamesContractResolver() };
Accepted answer did not work for me. In .netcore, I got it working with...
services.AddMvc(c => { .... }).AddJsonOptions(options => { options.SerializerSettings.Formatting = Formatting.Indented; .... })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With