In this article I will explain with an example, how to use
Newtonsoft library in
ASP.Net Core (.Net Core 3).
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-PackageMicrosoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.1.32
Once the package is installed, a success message will be displayed.
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.
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();
});
}