包含循环,并且如果禁用了引用跟踪,json.net和webapi,则无法序列化 [英] contains cycles and cannot be serialized if reference tracking is disabled, json.net and webapi

查看:71
本文介绍了包含循环,并且如果禁用了引用跟踪,json.net和webapi,则无法序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了错误:

 Object graph for type 'System.Collections.Generic.List`1[[Proj.Model.Prom, Proj.Model, 
 Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' contains cycles and cannot be 
 serialized if reference tracking is disabled.

阅读有关内容,似乎是序列化程序,但Json.Net声称是解决方案,我已阅读WebApi,而Framework 4.5默认具有它。那么默认情况下会来吗?如果是这样,为什么我仍然出现该错误?

Reading about that, seems to be the serializer, but Json.Net claims to be the solution and I've read WebApi and Framework 4.5 has it by default. So Is it coming by default? If so, Why I'm still getting that error?

谢谢!

编辑:添加代码

using System;
using System.Collections.Generic;
using System.Data.Spatial;

namespace Proj.Model
{
    public class Prom
    {
        public Prom()
        {
            this.Stores = new List<Store>();
            this.Branches = new List<Branch>();
            this.Products = new List<Product>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public DbGeography Location { get; set; }
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public int StateId { get; set; }
        public int CategoryId { get; set; }

        public virtual ICollection<Store> Stores { get; set; }
        public virtual ICollection<Branch> Branches { get; set; }
        public virtual ICollection<Product> Products { get; set; }

        public virtual Category Category { get; set; }
        public virtual State  State  { get; set; }

    }
}

using System;
using System.Collections.Generic;

namespace Proj.Model
{
    public class Category
    {
        public Category()
        {
            this.Proms = new List<Prom>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public virtual ICollection<Prom> Proms { get; set; }
    }
}

然后运行类似的操作会返回错误

Then running something like this returns the error

public IEnumerable<Category> GetList(int estadoId, string idTiposTarjetasList)
{
    var ids = "1,2,3,4".Split(',');
    var intIds = ids.Select(int.Parse);

    var Categories = Uow.Categorias.GetAllIncluding(c => c.Proms).ToList();
    foreach (var category in Categories)
    {
        var proms = category.Proms.Where(p => intIds.Contains(p.Id) && p.StateId == stateId).ToList();
        category.Proms = proms;
    }
    return Categories
}


推荐答案

默认情况下,WebApi将 PreserveReferencesHandling设置为无。

By default, WebApi set the 'PreserveReferencesHandling' to None.

您可以在WebApiConfig.cs中配置Json.NET SerializerSettings:

You can configure the Json.NET SerializerSettings in WebApiConfig.cs:

config.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = 
    Newtonsoft.Json.PreserveReferencesHandling.All;

这篇关于包含循环,并且如果禁用了引用跟踪,json.net和webapi,则无法序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆