Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.Net Core 1.1解决方案中检测到自引用循环_C#_Asp.net Core_Json.net_Object Graph - Fatal编程技术网

C# 在ASP.Net Core 1.1解决方案中检测到自引用循环

C# 在ASP.Net Core 1.1解决方案中检测到自引用循环,c#,asp.net-core,json.net,object-graph,C#,Asp.net Core,Json.net,Object Graph,尽管我已经看完了这篇文章,我还是不断地发现错误 检测到类型为“…”的属性“…”的自引用循环。路径'[4]..[0]' 我已将此添加到我的Startup.cs: services.AddMvc() .AddJsonOptions(opt => opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore ); 还有什么会导致引用循环错误 编辑

尽管我已经看完了这篇文章,我还是不断地发现错误

检测到类型为“…”的属性“…”的自引用循环。路径'[4]..[0]'

我已将此添加到我的
Startup.cs

services.AddMvc()
    .AddJsonOptions(opt => 
        opt.SerializerSettings.ReferenceLoopHandling = 
            ReferenceLoopHandling.Ignore
    );
还有什么会导致引用循环错误

编辑: 对评论中问题的回答。。。 受影响的类别包括:

public partial class GuidingSymptom
    {
        public GuidingSymptom()
        {
            VideosGuidingSymptoms = new HashSet<VideosGuidingSymptoms>();
        }
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [MaxLength(70)]
        [Required]
        public string Name { get; set; }
        public string Description { get; set; }

        public int SeverityId { get; set; }
        public int? DiagnoseId { get; set; }

        [InverseProperty("GuidingSymptom")]
        public virtual ICollection<VideosGuidingSymptoms> VideosGuidingSymptoms { get; set; }
        [ForeignKey("DiagnoseId")]
        [InverseProperty("GuidingSymptom")]
        public virtual Diagnose Diagnose { get; set; }
        [ForeignKey("SeverityId")]
        [InverseProperty("GuidingSymptom")]
        public virtual GuidingSymptomSeverity Severity { get; set; }
    }

public partial class VideosGuidingSymptoms
{
    public int VideoId { get; set; }
    public int GuidingSymptomId { get; set; }

    [ForeignKey("GuidingSymptomId")]
    [InverseProperty("VideosGuidingSymptoms")]
    public virtual GuidingSymptom GuidingSymptom { get; set; }
    [ForeignKey("VideoId")]
    [InverseProperty("VideosGuidingSymptoms")]
    public virtual Video Video { get; set; }
}
公共部分类引导症状
{
公共引导症状()
{
VideosGuidingSymptoms=新哈希集();
}
[关键]
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int Id{get;set;}
[MaxLength(70)]
[必需]
公共字符串名称{get;set;}
公共字符串说明{get;set;}
public int SeverityId{get;set;}
公共int?诊断ID{get;set;}
[反向属性(“导向症状”)]
公共虚拟ICollection VideosGuidingSymptoms{get;set;}
[外键(“诊断ID”)]
[反向属性(“导向症状”)]
公共虚拟诊断诊断{get;set;}
[外键(“SeverityId”)]
[反向属性(“导向症状”)]
公共虚拟向导严重性{get;set;}
}
公共部分课程视频引导症状
{
public int VideoId{get;set;}
public int GuidingSymptomId{get;set;}
[ForeignKey(“GuidingID”)]
[反向属性(“视频引导症状”)]
公共虚拟引导症状引导症状{get;set;}
[外键(“视频ID”)]
[反向属性(“视频引导症状”)]
公共虚拟视频{get;set;}
}

我找到了解决方案

[JsonIgnore]

受影响属性的注释。但是,我认为在使用
ReferenceLoopHandling.Ignore

时,这是不必要的。一些序列化框架不允许这样的循环。例如,如果遇到循环,Json.NET将抛出以下异常

Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'Blog' with type 'MyApplication.Models.Blog'.
如果您使用的是ASP.NET核心,则可以将Json.NET配置为忽略在对象图中找到的循环。这是在
Startup.cs
中的
ConfigureServices(…)
方法中完成的

public void ConfigureServices(IServiceCollection services)
{
    ...

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

    ...
}

显示违规行为的定义class@Nkosi添加了问题的答案可能重复您可以查看第页的我的答案。