12

伙计们,我与名为 user 的同一个类有一对多的关系,
我在 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":{"Message":"发生错误。","ExceptionMessage":"检测到类型为 'System.Data.Entity.DynamicProxies 的自引用循环.Friend_E5C79F4736EB2750392FFC7061B18E9C12F15F9583409603C75C5D1B1F7358D4'。路径'Friends1[0].User.Friends'。","ExceptionType":"Newtonsoft.Json.JsonSerializationException","StackTrace":"在 Newtonsoft.Json.Serializer.JsonSerializer。CheckForCircularReference(JsonWriter writer,Object value,JsonProperty property,JsonContract contract,JsonContainerContract containerContract,JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IWrappedCollection values,JsonArrayContract contract,JsonProperty member,JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, 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.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, 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 at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter。SerializeList(JsonWriter writer, IWrappedCollection values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter 编写器,对象值,JsonObjectContract 合同,JsonProperty 成员,JsonContainerContract 集合合同,JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue (JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member,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)"}}序列化(JsonWriter jsonWriter,对象值)\r\n 在 System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.b__c()\r\n 在 System.Threading.Tasks.TaskHelpers.RunSynchronously(Action 操作,CancellationToken 令牌)"}}序列化(JsonWriter jsonWriter,对象值)\r\n 在 System.Net.Http.Formatting.JsonMediaTypeFormatter.<>c__DisplayClassd.b__c()\r\n 在 System.Threading.Tasks.TaskHelpers.RunSynchronously(Action 操作,CancellationToken 令牌)"}}

4

2 回答 2

31

尝试更改 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
于 2013-10-29T16:58:20.823 回答
13

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

于 2014-02-24T22:17:45.997 回答