While converting a previous piece of code, written using Mvc 4.x I found that the output of the Json previously, when set no option, was unchanged, so a class with a property say, Name would output in JSON as ‘Name’.

However, ASP.NET Core Mvc
when returning JSon results, it now defaults to camelCasing.

 

My output had to be:

{
    "page": 1,
    "records": 2,
    "rows": [
        {
            "Id": 2,
            "Name": "asdfasdf",
            "UrlSlug": "asdfasdf",
            "Description": "asdf"
        },
        {
            "Id": 1,
            "Name": "Bkaat",
            "UrlSlug": "Blaat",
            "Description": "zdf
asdfasdf asdf a"
        }
    ],
    "total": 1
}

but it was camelCased.

 

So, HOW to set this to the previous behavior (its old software, so I won’t change everything)

Simple like this:

public void ConfigureServices(IServiceCollection services)

{

    services.AddMvc().AddJsonOptions(options => options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver() { NamingStrategy = new DefaultNamingStrategy()});

 

And finding further options, you also can set it to SnakeCaseNamingStrategy (this is like ‘url_slug’ etc)

And of course, CamelCaseNamingStrategy, which is the default.

I hope this saves you some time, and you don’t need to implement a custom contract resolver.