我有一个错误:"ExceptionMessage": "Self referencing loop detected for property 'Posto' with type 'UfficioServer.Posto'. Path '[0].Dipendente[0]'.",
当我调用我的网络 API 时,我需要一份与 Dipendente 关联的 Posti 列表......
public List<Posto> GetAllPosti()
{
try
{
List<Posto> p = new List<Posto>();
UfficioPostiEntities1 db = new UfficioPostiEntities1();
db.Configuration.ProxyCreationEnabled = false;
var posto = db.Posto.Include("Dipendente").ToList();
var x = db.Posto.ToList();
return x;
}
有人可以帮助我吗?
若要避免此错误,应将以下内容添加到 Global.asax 中的Application_Start:
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter
.SerializerSettings
.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
这是因为(根据文档(
ReferenceLoopHandling.Ignore
:Json.NET 将忽略 引用循环,而不是序列化它们。对象第一次 遇到它将像往常一样序列化,但如果对象是 作为自身的子对象遇到序列化程序将跳过 序列化它。