相关疑难解决方法(0)

JSON.NET错误检测到类型的自引用循环

我试图序列化从实体数据模型.edmx自动生成的POCO类,当我使用时

JsonConvert.SerializeObject 
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

错误发生类型System.data.entity检测到的自引用循环.

我该如何解决这个问题?

serialization json json.net

458
推荐指数
9
解决办法
27万
查看次数

JsonException: 检测到不支持的可能的对象循环。这可能是由于循环或对象深度大于

在我的 web api 中,当我运行项目以从数据库获取数据时出现此错误 .net core 3.1

JsonException: 检测到不支持的可能的对象循环。这可能是由于循环或对象深度大于最大允许深度 32。

这些是我的代码我的模型

 public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string ProductText { get; set; }
    public int ProductCategoryId { get; set; }
    [JsonIgnore]
    public virtual ProductCategory ProductCategory { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的 productCategory 类是:

 public class ProductCategory
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string CatText { get; set; …
Run Code Online (Sandbox Code Playgroud)

c# api asp.net-core-mvc asp.net-core

25
推荐指数
5
解决办法
3万
查看次数

.net核心:不完整的JSON响应

我正在尝试构建简单的API用于培训,在我的数据库中我获得了用户(名字,姓氏,电子邮件密码list<sports>)和体育(名称,用户ID).一切都没关系,当我想要获得我的用户时,我得到了一个填充了体育的对象.但是JSON响应是不完整的,它在中间被"切断".

[{"firstName":"Nicolas","lastName":"Bouhours","email":"n.bouh@test.com","password":"nico@hotmail.fr","sports":[{"name":"Trail","userId":1
Run Code Online (Sandbox Code Playgroud)

这是我的控制器:

// GET: api/Users
[HttpGet]
public IEnumerable<User> GetUsers()
{
    var users = _context.Users.Include(u => u.Sports).ToList();
    return users;
}
Run Code Online (Sandbox Code Playgroud)

我的模特:

public class Sport : BaseEntity
{
    public string Name { get; set; }

    public int UserId { get; set; }
    public User User { get; set; }
}

public class User : BaseEntity
{
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String Email { get; set; }
    public String …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc .net-core asp.net-core

15
推荐指数
2
解决办法
4571
查看次数

处理ASP.NET 5中的JSON循环引用异常

所以我在ASP.NET 5中使用Web API.在某些时候我的应用程序停止工作,只显示"Bad Gateway"IIS错误页面(我在IIS Express中运行它,由F5).我花了一段时间来弄清问题是什么 - 我在我的Web API方法返回的类中引入了一个循环引用,如下所示:

public class CircularParent
{
    public CircularChild Data;

    public CircularParent()
    {
        Data = new CircularChild(this);
    }
}

public class CircularChild
{
    public CircularParent Owner { get; set; }

    public CircularChild(CircularParent owner)
    {
        Owner = owner;
    }
}
Run Code Online (Sandbox Code Playgroud)

结果是JsonSerializationException.我的问题不是如何解决它,而是如何在将来处理这种情况.我怎么处理这样的例外?或者至少如何记录它或只是看到它记录在某个地方?UseDeveloperExceptionPage()没有帮助.UseExceptionHandler(errorApp => errorApp.Run(...))也没有帮助,执行没有进入errorApp.Run().调试器不会在异常处中断.我得到的所有IIS都是相当无法提供信息的"Bad Gateway"页面.

c# iis json.net asp.net-core

4
推荐指数
1
解决办法
4248
查看次数

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

虽然我已经按照这里的文章进行操作,但我不断收到错误消息

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

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

services.AddMvc()
    .AddJsonOptions(opt => 
        opt.SerializerSettings.ReferenceLoopHandling = 
            ReferenceLoopHandling.Ignore
    );
Run Code Online (Sandbox Code Playgroud)

还有什么可能导致参考循环错误?

编辑:在评论中回答问题......受影响的课程是:

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")] …
Run Code Online (Sandbox Code Playgroud)

c# json.net object-graph asp.net-core

3
推荐指数
1
解决办法
3812
查看次数