Microsoft.AspNetCore.Mvc.NewtonsoftJson 6.0.2 is not compatible with net5.0
Asked Answered
C

4

6

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();
Crump answered 10/2, 2022 at 3:41 Comment(7)
Why do you want 6.0.2? That's a very, very old version. We're on version 13 now (and v13 does work with ASP.NET Core), so why can't you use version 13?Heulandite
We're currently at version 13.0.1 Why do you want to use an ancient version? Try the new version, it should most likely work the same.Pak
@Heulandite - well how do I install 13.0.1 with net5.0?Crump
Right click on project, nuget packages, search for jsonPak
@Pak - So I see Newtonsoft.Json 13.0.1, but the tutorial indicates that I should be using Microsoft.AspNetCore.Mvc.NewtonsoftJson. Does it make a difference?Crump
@JohnBeasley Well, why are you following a clearly outdated tutorial?Heulandite
That's a different thing altogether. That's not a replacement for Newtonsoft.Json: it's something that integrates it with ASP.NET Core. Look at the dependencies of it - it actually depends on Newtonsoft.Json v13.0.1! So your question you told us you were installing something different from what you were actually trying to install. Precision matters - make sure you accurately describe your situation! The v6.0.2 version of that package supports .NET 6 and later. So why not use .NET 6?Galitea
G
22

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.

Galitea answered 10/2, 2022 at 4:22 Comment(1)
Thank you for your help. I am currently using Visual Studio 2019 which appears to only have .NET 3.1 and .NET 5 available. I will have to get a more recent version I guess.Crump
B
9

I faced this problem at today. My project's .NET version is 6.0.

For .NET 6.0 version, we must use this library Microsoft.AspNetCore.Mvc.NewtonsoftJson from Nuget.

For example: We must run below command for add Microsoft.AspNetCore.Mvc.NewtonsoftJson library to our project:

dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson --version 6.0.12

For other versions, you can see this url: Microsoft.AspNetCore.Mvc.NewtonsoftJson

Butte answered 15/12, 2022 at 9:25 Comment(0)
C
0

I believe he uses version 3.1.9 in the tutorial. You can change the version of the package (right beside the Install button, Version: ) to 3.1.9 and it should be good :)

enter image description here

Ciri answered 12/7, 2022 at 15:12 Comment(1)
This wouldn't be a good answer even if there wasn't a good answer accepted already. You can't use a .NET Framework assembly or NuGet package in a .NET Corea application. There was no .NET Core or .NET Standard in 2014, when JSON.NET was releasedRecite
I
0

Firs of all see what .Net version you are using in your program if its .Net 6 you should use Microsoft.AspNetCore.Mvc.NewtonsoftJson version 6.0.2 if its .Net 7 you can use the link that was referenced above to install it for .net 7, you get the point. Im running on .net 6 so installing Microsoft.AspNetCore.Mvc.NewtonsoftJson version 6.0.2 solved my issue. Quick note, Visual studio 2022 does not provide those versions except version 8 for .net 8 so you can install with command line in cmd by running this

dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson --version 6.0.2-preview.3.23177.8

or any other version you need!

Irreversible answered 6/5, 2023 at 20:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.