Handling JSON circular reference exception in ASP.NET 5

前端 未结 3 579
南旧
南旧 2020-12-07 03:14

So I\'m playing around with Web API in ASP.NET 5. At some point my app stopped working, only showing \"Bad Gateway\" IIS error page (I run it in IIS Express, by F5). It took

相关标签:
3条回答
  • 2020-12-07 03:27

    Try to add Newtonsoft.Json in the latest version 8.0.1-beta3 to dependencies in package.json and to use use

    services.AddMvc()
        .AddJsonOptions(options => {
            options.SerializerSettings.ReferenceLoopHandling =
                Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        });
    

    See the issue for more details.

    0 讨论(0)
  • 2020-12-07 03:33

    For ASP.NET Core 2.2 use the Configure method in Startup.cs

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        JsonConvert.DefaultSettings = () => new JsonSerializerSettings
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        };
    }
    
    0 讨论(0)
  • 2020-12-07 03:35

    For ASP.NET Core:

    var mvc = services.AddMvc(options =>
    {
       //mvc options
    });
    
    mvc.AddJsonOptions(options =>
    {
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });
    
    0 讨论(0)
提交回复
热议问题