처음으로 내 아키텍처에 대해 알려 드리고 싶습니다. ApiController 기능Asp.Net Mvc Web Api Json 형식화 자 문제
: 이야기 테이블에 대한 나의 이야기 클래스는public class Tale
{
[ScaffoldColumnAttribute(false)]
public int TaleId { get; set; }
public string TaleName { get; set; }
public string Content { get; set; }
public string VoicePath { get; set; }
public virtual ICollection<Category> Category { get; set; }
}
그 ı 같이 분류 표
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Tale> Tales { get; set; }
}
에 대한 나의 카테고리 클래스 내 TalesCategory 테이블
modelBuilder.Entity<Tale>()
.HasMany<Category>(u => u.Category)
.WithMany(r => r.Tales)
.Map(c => c.ToTable("TalesCategory")
.MapLeftKey("TaleKey")
.MapRightKey("CategoryKey"));
그리고 그것의 내 TaleController를 만들
public IEnumerable<Tale> GetAllTales()
{
return TaleService.FindAllTale();
}
내 WebApiConfig
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
나는 "/ API/이야기"나는 내 이야기의 목록 싶지만이 오류을 작성하는 경우. 나는 JsonMediaFormatter를 써야한다고 생각하고 약간의 코드를 시도했지만 성공하지 못했습니다. 내가 무엇을 할 수 있을지? .NET Framework 4.5를 사용합니다.
{"Message":"An error has occurred.","ExceptionMessage":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Tale_B48C4EAA8B3983ECA938C57C1764611B3C06FAC3348891DAC636EBEBF05EA8E2'. Path '[0].Category[0].Tales'.","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":" konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n konum: Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n konum:
의 중복 가능성 http://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop -detected-for-type) – CodeCaster