检测到属性asp.net web api

时间:2015-12-22 22:18:38

标签: json asp.net-mvc entity-framework asp.net-web-api

我在访问联系人对象的get方法时收到无限循环错误。我正在广泛访问已在其他项目中使用的数据模型,我认为这不是问题。

如果有其他信息可以提供帮助,请告诉我。

我通过网址http://localhost:65207/Api/contacts

访问了getContacts

这是方法

return db.contacts.Include(c => c.businesscontacts);

如果我应用take(1)

,查询会成功返回
return db.contacts.Take(1).Include(c => c.businesscontacts);

堆栈跟踪

  

{"消息":"发生了错误。"," ExceptionMessage":" The   ' ObjectContent`1'类型无法序列化响应正文   内容类型' text / html;   字符集= UTF-8'"" ExceptionType":" System.InvalidOperationException""堆栈跟踪":空,"的InnerException& #34; {"消息":"安   错误已经发生。"," ExceptionMessage":"自我引用循环   检测到财产'联系'与类型   ' System.Data.Entity.DynamicProxies.contact_86EA38F6B924D1435BCF3A7E50D07D29E0003280D2026E3A4AF9F36C87F1F214&#39 ;.   路径   ' [1] .businesscontacts [0]'"" ExceptionType":" Newtonsoft.Json.JsonSerializationException""堆栈跟踪&# 34;:"   在   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter   writer,Object value,JsonProperty属性,JsonContract契约,   JsonContainerContract containerContract,JsonProperty   containerProperty)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter   writer,Object value,JsonContainerContract contract,JsonProperty   成员,JsonProperty财产,JsonContract& memberContract,Object&   memberValue)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter   writer,Object value,JsonObjectContract契约,JsonProperty   成员,JsonContainerContract collectionContract,JsonProperty   containerProperty)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter   writer,Object value,JsonContract valueContract,JsonProperty成员,   JsonContainerContract containerContract,JsonProperty   containerProperty)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter   writer,IEnumerable值,JsonArrayContract契约,JsonProperty   成员,JsonContainerContract collectionContract,JsonProperty   containerProperty)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter   writer,Object value,JsonContract valueContract,JsonProperty成员,   JsonContainerContract containerContract,JsonProperty   containerProperty)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter   writer,Object value,JsonObjectContract契约,JsonProperty   成员,JsonContainerContract collectionContract,JsonProperty   containerProperty)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter   writer,Object value,JsonContract valueContract,JsonProperty成员,   JsonContainerContract containerContract,JsonProperty   containerProperty)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter   writer,IEnumerable值,JsonArrayContract契约,JsonProperty   成员,JsonContainerContract collectionContract,JsonProperty   containerProperty)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter   writer,Object value,JsonContract valueContract,JsonProperty成员,   JsonContainerContract containerContract,JsonProperty   containerProperty)\ r \ n at   Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter   jsonWriter,Object value,Type objectType)\ r \ n at   Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter   jsonWriter,Object value,Type objectType)\ r \ n at   Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter,Object   价值)\ r \ n at   System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型   type,Object value,Stream writeStream,Encoding   有效编码)\ r \ n at   System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(类型   type,Object value,Stream writeStream,Encoding   有效编码)\ r \ n at   System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型   type,Object value,Stream writeStream,HttpContent content)\ r \ n at   System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(类型   type,Object value,Stream writeStream,HttpContent内容,   TransportContext transportContext,CancellationToken   cancellationToken)\ r \ n ---从前一个位置开始的堆栈跟踪结束   抛出异常的地方--- \ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)\ r \ n at   System.Runtime.CompilerServices.TaskAwaiter.GetResult()\ r \ n at   System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext()"}}

1 个答案:

答案 0 :(得分:1)

问题很可能是某些BusinessContacts包含对Contacts的引用...然后您最终会在BusinessContacts和Contacts之间跳转。

请查看此问题以获取更多信息:

How Do You "Really" Serialize Circular Referencing Objects With Newtonsoft.Json?