我试图将数据从.net API发送到angular,以便我编写了一个如下所述的简单API,
在模型中(从实体框架生成):
public partial class LKEmployeeData
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public LKEmployeeData()
{
this.AssetTransactions = new HashSet<AssetTransaction>();
}
public string SamAccountName { get; set; }
public string DisplayName { get; set; }
public string Title { get; set; }
public string physicalDeliveryOfficeName { get; set; }
public string Department { get; set; }
public string EmailAddress { get; set; }
public string telephoneNumber { get; set; }
public string Manager { get; set; }
public string location { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<AssetTransaction> AssetTransactions { get; set; }
}
在控制器中:
public IEnumerable<LKEmployeeData> Get()
{
using(LKITInventoryEntities db=new LKITInventoryEntities())
{
return db.LKEmployeeDatas.ToList();
}
}
只要我在模型中只有一个表,我就可以在浏览器中正确获取Json格式,但是当我向视图中添加更多表时,就会出现以下异常
发生错误。 'ObjectContent`1'类型无法序列化响应主体 内容类型为'application / xml; charset = utf-8'。 System.InvalidOperationException 有一个错误 发生。类型 'System.Data.Entity.DynamicProxies.LKEmployeeData_AFA464720CA28CF4277D0AC8BD938EF7F8AD656E661EFE760F091A3000A1C88F' 带有数据合同名称 'LKEmployeeData_AFA464720CA28CF4277D0AC8BD938EF7F8AD656E661EFE760F091A3000A1C88F:http://schemas.datacontract.org/2004/07/System.Data.Entity.DynamicProxies' 不是预期的。如果您正在考虑使用DataContractResolver 使用DataContractSerializer或将静态未知的任何类型添加到 已知类型的列表-例如,通过使用KnownTypeAttribute 属性,或将其添加到传递给的已知类型列表中 序列化器。 System.Runtime.Serialization.SerializationException 在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,Boolean verifyKnownType,RuntimeTypeHandle,声明的TypeHandle,类型 声明的类型) System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter,对象obj,RuntimeTypeHandle,objectTypeHandle,类型 objectType,Int32声明的TypeID,RuntimeTypeHandle clarifiedTypeHandle,类型为declaredType) System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter,Object obj,Boolean isDeclaredType,Boolean writeXsiType, Int32声明为TypeID,RuntimeTypeHandle声明为TypeHandle),位于 WriteArrayOfLKEmployeeDataToXml(XmlWriterDelegator,Object, XmlObjectSerializerWriteContext,CollectionDataContract),位于 System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter,Object obj,XmlObjectSerializerWriteContext上下文) System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj, RuntimeTypeHandle声明为TypeHandle),位于 System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj,Boolean verifyKnownType,RuntimeTypeHandle,声明的TypeHandle,类型 声明的类型) System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract,XmlWriterDelegator xmlWriter,Object obj, RuntimeTypeHandle originalDeclaredTypeHandle,键入graphType)位于 System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer,对象图,DataContractResolver dataContractResolver)位于 System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer,对象图,DataContractResolver dataContractResolver)位于 System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer,对象图,DataContractResolver dataContractResolver)位于 System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter 作者,对象图)在 System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type 类型,对象值,流writeStream,HttpContent内容) System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type 类型,对象值,流writeStream,HttpContent内容, TransportContext transportContext,CancellationToken cancelToken)---从先前位置开始的堆栈结束跟踪 引发了异常--- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务) System.Web.Http.WebHost.HttpControllerHandler.d__22.MoveNext()
我也尝试了以下解决方案来解决此问题,但未成功
试图在Global.asax.cs
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
还尝试在WebApiConfig.cs
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
config.Formatters.Remove(config.Formatters.XmlFormatter);
此问题的最佳解决方案是什么