JsonSerializerSettings and Asp.Net Core

Trying to set JsonOutputFormatter options:

var jsonFormatter = (JsonOutputFormatter) options.OutputFormatters.FirstOrDefault(f => f is JsonOutputFormatter);
if (jsonFormatter != null)
{
    jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}

or

mvcBuilder.AddJsonOptions(jsonOptions =>
    {
        jsonOptions.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    });

But as soon as I add this, I get:

MissingMethodException: Method not found: 'Newtonsoft.Json.JsonSerializerSettings Microsoft.AspNet.Mvc.Formatters.JsonOutputFormatter.get_SerializerSettings()'.

I'm using the standard Microsoft.AspNet.Mvc.Formatters.Json (6.0.0-rc1-final)

Edit: Solved it by installing Newtonsoft.Json 6.0.6 (which downgrades all other references)

Anyone got that already? Thanks..


Solution 1:

.Net Core 1.0 RTM comes with CamelCase formatting out-of-the-box. This is a behavior change from RC2. However, if you need to modify it, try this snippet:

services.AddMvc()
        .AddJsonOptions(opt =>
    {
        var resolver  = opt.SerializerSettings.ContractResolver;
        if (resolver != null)
        {
            var res = resolver as DefaultContractResolver;
            res.NamingStrategy = null;  // <<!-- this removes the camelcasing
        }
    });

More information here.

For dotnet core 1.0.1:

  services
            .AddMvcCore()
            .AddJsonFormatters(o => o...);

Solution 2:

I assume you are using ASP.Net Core and you should use "Microsoft.AspNetCore.Mvc":

So replace this:

"Microsoft.AspNet.Mvc": "6.0.0-rc1-final"

by this:

"Microsoft.AspNetCore.Mvc": "1.0.0-rc2-final"