Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I solve AddJsonOptions does not contain definition of SerializerSettings - .NET

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)

Thanks in Advance,

like image 746
David Cardoso Avatar asked Mar 19 '20 19:03

David Cardoso


1 Answers

Solved.

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

Hope this helps.

NuGet: Microsoft.AspNetCore.Mvc.NewtonsoftJson

like image 106
David Cardoso Avatar answered Oct 21 '22 02:10

David Cardoso