How do I solve AddJsonOptions does not contain definition of SerializerSettings
Asked Answered
E

2

21

hope someone can help me, I've been searching, and haven't been able to find a solution. Might as well be something basic, I just can't find a solution.

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
    .AddJsonOptions(opt =>
    {
        opt.SerializerSettings.ReferenceLoopHandLing = 
            Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    });

This code is trying to fix a problem with reference looping. I'm following a tutorial on building a webapp with .NET and Angular CLI. On the tutorial, it's shown, the loop error, that I also got on my code. Trying to follow the solution (code above) I got an error on the SerializerSettings, saying that JsonOptions do not contain a definition for such.

I tried:

adding [Obsolete] (suggested by visual studio),

installing the Microsoft.AspNetCore.MVC.Formatters.Json nuget package (which VS informed it was doing nothing when added), (saw this solution on Documentation and Here com StackOverflow)

Tried ReferenceLoopHandling as for Newtonsoft Json.Net documentation (I might not have used it properly so if anyone feels this is the way out, please show me)

Euterpe answered 19/3, 2020 at 19:2 Comment(2)
Hi, I;m having a same problem on this, did you already fix this issue? can you share how did you fix this?Siobhan
Hey @eljon_i3, sorry for the delay, for me, the anwser is as stated below ;-)Euterpe
E
28

Solved.

services.AddMvc().AddNewtonsoftJson(o => 
{
    o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});   

Hope this helps.

NuGet: Microsoft.AspNetCore.Mvc.NewtonsoftJson

Euterpe answered 13/4, 2020 at 13:24 Comment(4)
Solution was changing ".AddJsonOptions" to ".AddNewtonsoftJson", took me a while to see it.Mons
For .NET 5, I needed to reference Microsoft.AspNetCore.Mvc.NewtonsoftJson. See https://mcmap.net/q/100494/-where-did-imvcbuilder-addjsonoptions-go-in-net-core-3-0.Dyal
my arse is saved thank you dear sirOrose
Installing Microsoft.AspNetCore.Mvc.NewtonsoftJson was the solutionObscene
D
0

In this link: https://learn.microsoft.com/es-es/dotnet/standard/serialization/system-text-json/migrate-from-newtonsoft?pivots=dotnet-6-0

the microsoft documentation says that in .Net 6.0 the option with the same behavior is services .AddMvc() .AddJsonOptions( options => options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles); enter image description here

Drona answered 21/2 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.