بررسی خطای Circular References در ASP.NET MVC Json Serialization
نویسنده: مجتبی کاویانی
تاریخ: ۱۳۹۲/۰۹/۱۸ ۰:۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
[HttpGet]
public JsonResult Get(int id)
{
return Json(repository.Find(id), JsonRequestBehavior.AllowGet);
} A circular reference was detected while serializing an object of type ‘System.Data.Entity.DynamicProxies.ItemCategory_A79…’
public class Item
{
public int Id { get; set; }
[ForeignKey]
public int ItemId { get; set; }
public string Name { get; set; }
public ICollection<Item> Items { get; set; }
} [HttpGet]
public JsonResult List()
{
var data = repository.AllIncluding(itemcategory => itemcategory.Items);
var collection = data.Select(x => new
{
id = x.Id,
name = x.Name,
items = x.Items.Select(item => new
{
id=item.Id,
name = item.Name
})
});
return Json(collection, JsonRequestBehavior.AllowGet);
} GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize; GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;