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

前端 未结 2 1901
情书的邮戳
情书的邮戳 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.

提交回复
热议问题