In this article I will explain with an example, how to use Newtonsoft library in ASP.Net Core (.Net Core 3).
Note: For beginners in ASP.Net Core (.Net Core 3), please refer my article ASP.Net Core 3: Hello World Tutorial with Sample Program example.
 
 

Installing Newtonsoft package from Nuget

In order to install Newtonsoft library in ASP.Net Core (.Net Core 3), you will need execute the following command.
Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.1.32
 
Configuring Newtonsoft library in ASP.Net Core 3
 
Once the package is installed, a success message will be displayed.
Configuring Newtonsoft library in ASP.Net Core 3
 
 

Configuring Newtonsoft library

In order to configure Newtonsoft library in ASP.Net Core (.Net Core 3), please follow the below steps.
1. Open the Startup.cs file from the Solution Explorer window.
Configuring Newtonsoft library in ASP.Net Core 3
 
2. Inherit the following namespace.
using Newtonsoft.Json.Serialization;
 
3. Inside the ConfigureServices method of Startup.cs class, the AddNewtonsoftJson method of the services class is called where the ContractResolver is set to DefaultContractResolver object which will instruct the program to use Microsoft.AspNetCore.Mvc.NewtonsoftJson library for JSON serialization.
public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews()
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            });
}
 


Other available versions