001
001

Reputation: 65077

How to change json to camelCase in .NET Core 3.1.1?

After upgrading to 3.1.1 , AddNewsoftJson is missing, how to change the json casing format now?

services.AddControllers()
    .AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    });

Upvotes: 6

Views: 11063

Answers (3)

Nitesh Mandlik
Nitesh Mandlik

Reputation: 49

Using

new DefaultContractResolver() 

instead of

new CamelCasePropertyNamesContractResolver()

worked for me

Upvotes: 4

Anuraj
Anuraj

Reputation: 19598

It is moved to a nuget package.

dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson --version 3.1.1

Install this package and include the following namespace in the startup class - ConfigureServices method.

using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Serialization;

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers().AddNewtonsoftJson(options =>
    {
        options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    });
}

Upvotes: 16

ProgrammingLlama
ProgrammingLlama

Reputation: 38727

Based on this third-party blog, it appears to be a NuGet package now, so you'll have to install it in your project:

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.1.1

Your code should then work.

Upvotes: 0

Related Questions