Kas*_*Kas 11 c# json asp.net-mvc-4 asp.net-web-api
伙计我和同一个类有一对多关系叫做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
Run Code Online (Sandbox Code Playgroud)
这是我的GetMethod
[HttpGet]
public Models.User Authenticate()
{
try
{
return db.Users.SingleOrDefault(x => x.ID == 0 ) ;
}
catch
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我得到并返回一个用户实体,那么我得到了打击错误
任何人都可以帮我解决以下错误吗?
{"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.在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IWrappedCollection值,JsonArrayContract契约,JsonProperty成员,JsonContainerContract collectionContract,CheckForCircularReference(JsonWriter writer,Object value,JsonProperty属性,JsonContract contract,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \n) 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编写器,Object值,JsonObjectContract契约,JsonProperty成员,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 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中的containerContract,JsonProperty containerProperty)\ r \n.SerializeList(JsonWriter作家,IWrappedCollection值,JsonArrayContract合同,JsonProperty构件,JsonContainerContract collectionContract,JsonProperty containerProperty个)\ r \n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter作家,对象的值,JsonContract valueContract,JsonProperty构件,JsonContainerContract containerContract, JsonProperty containerProperty)\ r \n at 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成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Object value)\ r \n在Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value)\ r \n在System.Threading.Tasks.TaskHelpers的System.Net.Http.Formatting.JsonMediaTypeFormatter.<> c__DisplayClassd.b__c()\ r \n的Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter,Object value)\ r \n. RunSynchronously(Action action,CancellationToken token)"}}System.Threading.Tasks.TaskHelpers.RunSynchronously上的System.Net.Http.Formatting.JsonMediaTypeFormatter.<> c__DisplayClassd.b__c()\ r \n的Serialize(JsonWriter jsonWriter,Object value)\ r \n(Action action,CancellationToken token )"}}System.Threading.Tasks.TaskHelpers.RunSynchronously上的System.Net.Http.Formatting.JsonMediaTypeFormatter.<> c__DisplayClassd.b__c()\ r \n的Serialize(JsonWriter jsonWriter,Object value)\ r \n(Action action,CancellationToken token )"}}
ale*_*mac 31
尝试更改webApi格式化程序.在WebApiConfig.cs中添加以下行:
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
Run Code Online (Sandbox Code Playgroud)
并添加此行:
json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20836 次 |
| 最近记录: |