How to stop self-referencing loop in .Net Core Web API?

后端 未结 3 1099
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 21:00

I\'m having some issues which I\'m guessing are related to self-referencing using .NET Core Web API and Entity Framework Core. My Web API starting choking when I added .Inc

3条回答
  •  执笔经年
    2020-12-03 21:28

    If you are using ASP.NET Core 3.0, and experience that problem please install the NuGET package: Microsoft.AspNetCore.Mvc.NewtonsoftJson 3.0.0.

    To replace the new System.Text.Json which does not yet have the Reference Loop Handling do this in the Startup.cs, make sure that in the ConfigureServices, is included:

    If using the latest .Net Core 3.0 way:

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

    or the old way:

    services.AddMvc(option => option.EnableEndpointRouting = false)
           .AddNewtonsoftJson(options => 
                     options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore)
           .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
    

提交回复
热议问题