ASP.NET Web API Serialized JSON Error: “Self Referencing loop”

前端 未结 2 1902
情书的邮戳
情书的邮戳 2021-01-15 03:43

Been a long struggle with this one. I\'m working with an ASP.NET web API to provide clean and easy HTTP/JSON interaction with a database. I have an entity name Reservation w

相关标签:
2条回答
  • 2021-01-15 04:27

    The error was caused by the EF creating proxies for the foreign key objects, the default serializer (DataContractSerializer) errors when serializing with proxies. The solution is to add the following line to the Global.asax.cs file:

    GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 
    

    While @SKall's answer did solve the problem, it didn't solve it throughout the project, just to properties that I gave the IgnoreDataMember attribute.

    0 讨论(0)
  • 2021-01-15 04:29

    Mark the references with IgnoreDataMember attribute and let us know if that helped.

    [IgnoreDataMember]
    public virtual Equipment Equipment { get; set; }
    
    [IgnoreDataMember]
    public virtual ReservationState ReservationState { get; set; }
    
    0 讨论(0)
提交回复
热议问题