15

I have been beating my head against a wall with this one, trying to find out why it won't work. I haven't been able to find anything on why it won't work, so I am asking here.

I have a console application that is running on Asp.Net Core 3.0 Preview 3.

On this project I am getting a Json loop problem, which I know I can fix with setting the Reference Loop Handling in Startup to Ignore. However, I could only find information on setting it inside the .AddJsonOptions(), which doesn't appear to be in Asp.Net Core 3.0.

I went to the documentation for how to migrate from 2.1 to 3.0 and I found this

Even after changing my code accordingly

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

I still get an error saying: "Self referencing loop detected for property '[insert class name]' with type '[model name]'."

Where else can I set Json to ignore the loop reference?

Or what can I do to make this work?

Thank you in advanced

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
DalTron
  • 939
  • 3
  • 9
  • 22
  • 2
    Have you tried doing the serialization explicitly, i.e serialize the object/data in a method or in your controller action. like so `JsonConvert.SerializeObject(object, new JsonSerializerSettings().ReferenceLoopHandling = ReferenceLoopHandling.Ignore)` I believe this will take precedence over what you have in your startup.cs class – Ugo Okoro Mar 19 '19 at 17:29
  • looks similar: https://github.com/aspnet/AspNetCore/issues/8480 – Vein Mar 19 '19 at 17:31
  • 3
    FWIW, this type of thing generally becomes a non-issue if you use proper DTOs, instead of things like entity classes directly. With a DTO class(es), you can remove the reference loop entirely from the equation. – Chris Pratt Mar 19 '19 at 17:34
  • Thanks all of you, UgoOkoro that works thank you. However, I am going to go for creating DTOs like ChrisPratt said that should make this a non-issue. I had briefly thought about doing something like DTOs, but I didn't know there was a name for it and everything. Thank you guys. – DalTron Mar 20 '19 at 19:03
  • do you add app.useMvc() in configure method? – Haithem KAROUI Oct 08 '19 at 15:16
  • 1
    Please look at this [link](https://stackoverflow.com/a/58517316/4656623) for the solution. – Eduard Braun Oct 23 '19 at 07:16

2 Answers2

5
services.AddMvc().AddNewtonsoftJson(options=> options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#jsonnet-support

Sato
  • 51
  • 1
  • 6
4

As explained in detail here as part of ASP.NET Core 3.0, the team moved away from including Newtonsoft.Json by default.

You probably need to install Microsoft.AspNetCore.Mvc.NewtonsoftJson and use (note, I'm using .AddNewtonsoftJson() chained with .AddControllers()) something similar:

services.AddControllers()
    .AddNewtonsoftJson(x =>
            {
                x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            });
Filipe Madureira
  • 420
  • 4
  • 17