zoukankan      html  css  js  c++  java
  • 关于.Net Core生成JSON时错误:A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.

    此笔记记载了本人在.Net Core 5.0环境下生成Json数据时A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.的症状、排查及解决方案。

    环境

    .Net Core版本:5.0

    编译器:Visual Studio 2019,Rider2021.1.3

    症状

    在生成Json数据的时候会提示A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.的错误提示。

    解决方案

    造成此问题的原因是由于在生成Json对象的时候属性的循环引用导致的。

    可用如下方法进行解决

    1. 安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson 包

      可以从Nuget上进行下载并安装

    2. 安装完成后在 Startup.cs 文件中加入如下代码

      public void ConfigureServices(IServiceCollection services)
      {
          // .Net Core 5.0以下适用
          services.AddMvc(options => { options.Filters.Add<ApiModelCheckFilterAttribute>(); })
              .AddJsonOptions(options =>
              {
                  // 忽略属性的循环引用
              	options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
              });
          
          // .Net Core 5.0适用
          services.AddMvc(options => { options.Filters.Add<ApiModelCheckFilterAttribute>(); })
              .AddNewtonsoftJson(options =>
              {
                  // 忽略属性的循环引用
              	options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
              });
      }
      

    本文来自博客园,作者:一块白板,转载请注明原文链接:https://www.cnblogs.com/ykbb/p/15015265.html

  • 相关阅读:
    Oracle dbms_job
    ORACLE 表空间扩展
    面试技术岗,你真能讲明白技术吗?
    阿里巴巴高级技术专家章剑锋:大数据发展的 8 个要点
    技术管理管什么
    如何做到进人精挑细选,裁人快刀斩乱麻
    数据脱敏
    美团数据治理平台
    impala教学视频
    任正非的讲话
  • 原文地址:https://www.cnblogs.com/ykbb/p/15015265.html
Copyright © 2011-2022 走看看