'Microsoft.AspNetCore.Mvc.NewtonsoftJson 6.0.2 is not compatible with net5.0

I am using Visual Studio 2019 on a Mac, trying to start a REST API project.

Immediately got stuck when trying to install NewtonsoftJson 6.0.2

I was just following a tutorial, and version of NewtonsoftJson that was being using was 3.1.9.

Firstly - Is there a way to install 6.0.2?

Secondly - If 6.0.2 cannot be installed, is there a way to install the older version so I can proceed with this tutorial?

Edit

I added Newtonsoft.Json 13.0.1 per suggestions in the comments, but I am still getting the red line beneath the text below that reads AddNewtonsoftJson():

services.AddControllersWithViews().AddNewtonsoftJson();


Solution 1:[1]

It seems you've misstated in your question what you're installing. The package you're actually installing is not Newtonsoft.Json, but rather Microsoft.AspNetCore.Mvc.NewtonsoftJson. The AddNewtonsoftJson extension method comes from that library.

Newtonsoft.Json is a library for serializing/deserializing JSON. It's one of the most heavily used .NET libraries out there. In fact, it's the most downloaded package on Nuget.org.

Microsoft.AspNetCore.Mvc.NewtonsoftJson is a library that integrates Newtonsoft.Json with ASP.NET Core MVC so that it uses Newtonsoft.Json for its JSON serialization/deserialization needs. It depends on Newtonsoft.Json, which you can see by examining the dependencies of the package on nuget.org.

So, you say your app is .NET 5, and you're following a tutorial that uses Microsoft.AspNetCore.Mvc.NewtonsoftJson v6.0.2. If you look at the dependencies page on nuget.org (or better yet, on Fuget where it's a bit clearer), you'll see that v6.0.2 only runs on .NET 6. Therefore the tutorial you're following is for .NET 6.

So, you've got a couple possible options from here:

  1. You can install the latest version of Microsoft.AspNetCore.Mvc.NewtonsoftJson that supports running on .NET 5. That's v5.0.14, and it depends on Newtonsoft.Json v12.0.2. That's not the most recent (it came out in 2019), but it's not extremely old. Keep in mind there may be other differences in your .NET 5 app compared to the .NET 6 tutorial you're trying to run.

  2. You can start using .NET 6 to match your tutorial. That gets you on the newest version, which has a lot of nice improvements, and it's a Long Term Support version, so it will be supported much longer than .NET 5. This may require a newer version of Visual Studio, or you could use Visual Studio Code.

  3. You can find a different tutorial altogether that is specific to .NET 5.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1