In this article I will explain with an example, how to use
Newtonsoft library in
ASP.Net Core (.Net Core 8).
Installing Newtonsoft package from Nuget
In order to install
Newtonsoft library in
ASP.Net Core (.Net Core 8), you will need execute the following command.
Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 8.0.8
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 8), please follow the below steps.
1. Open the Program.cs file from the Solution Explorer window.
2. Inherit the following namespace.
using Newtonsoft.Json.Serialization;
3. Inside the
Program.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.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});