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

Installing Newtonsoft package from Nuget

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

Configuring Newtonsoft library

In order to configure Newtonsoft library in ASP.Net Core (.Net Core 5), please follow the below steps.
1. Open the Startup.cs file from the Solution Explorer window.
Using Newtonsoft as JSON Serializer in ASP.Net Core (.Net Core 5)
 
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