Csharp/C#教程:为什么在web api中返回一个具有一对多关系的实体会导致错误?分享


为什么在web api中返回一个具有一对多关系的实体会导致错误?

伙计我和同一个class级有一对多的关系叫做用户,
我在web apis get方法中返回一个用户实例,只要我没有,它就可以正常工作

User ID | Name 0 | A 1 | B Friends Table ( This table is used to build the one to many relationship ) User_ID | Friend_ID 1 | 0 0 | 1 

这是我的GetMethod

 [HttpGet] public Models.User Authenticate() { try { return db.Users.SingleOrDefault(x => x.ID == 0 ) ; } catch { return null; } } 

如果我得到并返回一个用户实体,那么我得到了打击错误

任何人都可以帮我解决以下错误吗?

{“Message”:“发生了错误。”,“ExceptionMessage”:“’ObjectContent`1’类型无法序列化内容类型’text / html; charset = utf-8’的响应正文。”,“ExceptionType “:”System.InvalidOperationException“,”StackTrace“:null,”InnerException“:{”消息“:”发生错误。“,”ExceptionMessage“:”使用类型’System.Data.Entity.DynamicProxies检测到自引用循环.Friend_E5C79F4736EB2750392FFC7061B18E9C12F15F9583409603C75C5D1B1F7358D4’。路径’Friends1 [0] .User.Friends’。“,”ExceptionType“:”Newtonsoft.Json.JsonSerializationException“,”StackTrace“:”at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer,Object在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IWrappedCollection值,JsonArrayContract契约,JsonProperty成员,JsonContainerCon)中的值,JsonProperty属性,JsonContract契约,JsonContainerContract containerContract,JsonProperty containerProperty) r n 在Newtonsoft.Json.Serialization中,在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty) r n中的集合collectionContract,JsonProperty containerProperty) r n。 JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty) r n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract)在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerPr)中的containerContract,JsonProperty containerProperty) r n 操作) r n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty) r n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList( JsonWriter writer,IWrappedCollection values,JsonArrayContract contract,JsonProtainerContract collectionContract,JsonProperty containerProperty) r n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty) ) r n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty) r n在Newtonsoft.Json.Serializa .JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty) r n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Object value) r n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value) r n在Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter,Object value) r n在System.Net.Http.Formatting.JsonMediaTypeFormatter。 c__DisplayClassd.b__c() r n在System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action,CancellationToken token)“}}

尝试更改webApi格式化程序。 在WebApiConfig.cs中添加以下行:

 var json = config.Formatters.JsonFormatter; json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; config.Formatters.Remove(config.Formatters.XmlFormatter); 

并添加此行:

 json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore 

[JsonIgnore]属性应用于您不希望序列化的导航属性。 它仍将序列化父实体和子实体,但只是避免了自引用循环。

上述就是C#学习教程:为什么在web api中返回一个具有一对多关系的实体会导致错误?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1039169.html

(0)
上一篇 2022年1月26日
下一篇 2022年1月26日

精彩推荐