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

6
  • 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 Commented Mar 19, 2019 at 17:29
  • looks similar: github.com/aspnet/AspNetCore/issues/8480 Commented Mar 19, 2019 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. Commented Mar 19, 2019 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. Commented Mar 20, 2019 at 19:03
  • 1
    Please look at this link for the solution. Commented Oct 23, 2019 at 7:16

2 Answers 2

6

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;
            });
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.