33

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..

Maxime Rouiller
  • 13,614
  • 9
  • 57
  • 107
Senj
  • 649
  • 1
  • 8
  • 18
  • 1
    Where you tried to insert the code fragment? What is `mvcBuilder`? Could you include the code of `ConfigureServices` method from `Startup.cs`, which you use? It's correct place to call `AddJsonOptions`. – Oleg Mar 03 '16 at 13:02
  • This just randomly started happening to me today. Honestly it was working yesterday which really leaves me puzzled. – lc. Apr 26 '16 at 09:51

2 Answers2

66

.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...);
Robert Cutajar
  • 3,181
  • 1
  • 30
  • 42
Jeson Martajaya
  • 6,996
  • 7
  • 54
  • 56
  • 1
    Thank you so much! I couldn't figure out where the automatic camel casing was happening. – John Edwards Sep 26 '16 at 15:52
  • 4
    Any idea what this call is in .Net core 1.1? – Levi Fuller Mar 12 '17 at 03:06
  • @LeviFuller it's the same - `AddJsonOptions` – chester89 Aug 10 '17 at 21:42
  • I had an already configured SerializeSettings override. Now I have to reproduce everything because I cannot directly set SerializerSettings. There plenty of you should not do that in asp.net core that are not particularly relevant. – Maly Lemire Oct 05 '17 at 23:08
  • We're moving to asp.net core and decided to use the default setting (camelCase) for all the internal stuff. However, we have a situation that for backwards compatibility to external system we need the output to be PascalCase just for a single controller. What is the best way to do this? – howardlo Mar 19 '19 at 23:49
1

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"
Maxim
  • 13,029
  • 6
  • 30
  • 45