当前位置: 首页 > 编程语言 > C#

为什么在webapi中返回一对多关系的实体会报错?分享

时间:2023-04-11 11:10:06 C#

为什么在webapi中返回一对多关系的实体会出错?伙计们,我与同一个名为Users的类有一对多关系,我在webapiget方法中返回一个User实例,只要我没有用户ID,它就可以正常工作|名称0|一个1|BFriends表(此表用于建立一对多关系)User_ID|Friend_ID1|00|1这是我的GetMethod[HttpGet]publicModels.UserAuthenticate(){try{returndb.Users.SingleOrDefault(x=>x.ID==0);}赶上{返回空;如果我得到并返回一个用户实体然后我得到命中错误任何人都可以帮我解决以下错误吗?{"Message":"发生错误。","ExceptionMessage":"类型'ObjectContent`1'无法序列化内容类型'text/html;charset=utf-8'的响应文本。","ExceptionType":"System.InvalidOperationException","StackTrace":null,"InnerException":{"Message":"发生错误。","ExceptionMessage":"检测到类型为'System.Data.Entity.DynamicProxies的自引用循环。Friend_E5C79F4736EB2750392FFC7061B18E9C5D75F15F603C875'路径'Friends1[0].User.Friends'。“,”ExceptionType“:”Newtonsoft.Json.JsonSerializationException“,”StackTrace“:”atNewtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriterwriter,ObjectinNewtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriterwriter,IWrappedCollection值,JsonArrayContract契约,JsonProperty成员,JsonContainerCon)中的值,JsonProperty属性,JsonContract契约,JsonContainerContract容器Contract,JsonPropertycontainerProperty)rn在Newtonsoft.Json.Serialization中,在Newtonsoft.Json.Serialization.JsonSerializerWriterInternalWriter(Serialization.JsonSerializerWriterValueer.Serializer),JsonContract值Contract,JsonProperty成员,JsonContainerContract容器Contract,JsonProperty容器Property)rn中的集合集合Contract,JsonProperty容器Property)rn。JsonSerializerInternalWriter.SerializeObject(JsonWriterwriter,对象值,JsonObjectContract契约,JsonProperty成员,JsonContainerContract集合Contract,JsonPropertycontainerProperty).Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriterwriter,Objectvalue,JsonObjectContractcontract,JsonProperty成员,JsonContainerContractcollectionContract,JsonPropertycontainerPr)中的containerContract,JsonPropertycontainerProperty)rn操作)rn在Newtonsoft.Json.Serialization.JsonWritizeSerializer(JsonWritizeSerializer)writer,Objectvalue,JsonContractvalueContract,JsonProperty成员,JsonContainerContractcontainerContract,JsonPropertycontainerProperty)rn在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriterwriter,IWrappedCollectionvalues,JsonArrayContractcontract,JsonProtainerContractcollectionContract,JsonPropertycontainerProperty)rn在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriterwriter,对象值,JsonContractvalueContract,JsonProperty成员,JsonContainerContractcontainerContract,JsonPropertycontainerContract,JsonPropertyncontainerContract)Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriterwriter,Objectvalue,JsonObjectContractcontract,JsonPropertymember,JsonContainerContractcollectionContract,JsonPropertycontainerProperty)rn在Newtonsoft.Json.Serializa.JsonSerializerInternalWriter.SerializeValue(JsonWriterwriter,对象值,JsonContractvalueContract,JsonPropertyJsonContainerContractcontainerContract,JsonPropertycontainerProperty)rn在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriterjsonWriter,对象值)rn在Newtonsoft.Json.JsonSserializer.SerializeInternal(JsonWriterjsonWriter,Objectvalue)rn在Newtonsoft.Json.JsonSerializer.Serialize(JsonWriterjsonWriter,Objectvalue)rn在System.Net.Http.Formatting.JsonMediaTypeFormatterc__DisplayClassd.b__c()rn在System.Threading.Tasks。TaskHelpers.RunSynchronously(Actionaction,CancellationTokentoken)"}}尝试更改webApi格式化程序。在WebApiConfig.cs中添加以下行:varjson=config.Formatters.JsonFormatter;json.SerializerSettings.PreserveReferencesHandling=Newtonsoft.Json.PreserveReferencesHandling。对象;config.Formatters.Remove(config.Formatters.XmlFormatter);并添加此行:json.SerializerSettings.ReferenceLoopHandling=Newtonsoft.Json.ReferenceLoopHandling.Ignore将[JsonIgnore]属性应用于您不想序列化的导航属性。它的父子实体还是会被序列化,只是避免了自引用循环以上是C#学习教程:为什么在webapi中返回一个一对多关系的实体会报错?分享以上所有内容,如果您有任何用到,需要进一步了解C#学习教程,希望大家多加关注——本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:

最新推荐
猜你喜欢