I can not use the 'services.AddControllers().AddNewtonsoftJson()'

Viewed 7608

I'm in Net core 3.1 and I'm trying to use AddNewtonsoftJson()

I'm getting:

"IMvcBuilder' does not contain a definition for 'AddNewtonsoftJson' and no accessible extension method 'AddNewtonsoftJson' accepting a first argument of type 'IMvcBuilder' could be found (are you missing a using directive or an assembly reference?)"

This is what I write in my ConfigureService:

services.AddControllers().AddNewtonsoftJson();

I already have installed NewtonSoft in my startup project.

2 Answers

The package Microsoft.AspNetCore.Mvc.NewtonsoftJson is no longer included in Microsoft.AspNetCore.App framework (aka. meta package in version 2.x) because the new System.Text.Json is now mature enough to replace Newtonsoft.Json as the built-in json (de)serializer. Therefore consider replacing all uses of Newtonsoft.Json with System.Text.Json for performance and futural compatibility.

Only if it is not possible, you could install the Microsoft.AspNetCore.Mvc.NewtonsoftJson package manually, while a direct reference to Newtonsoft.Json is no longer need, for Microsoft.AspNetCore.Mvc.NewtonsoftJson already references Newtonsoft.Json.

Related