【问题标题】:ASP.net webapi IHttpActionResult return complex JSONASP.net webapi IHttpActionResult 返回复杂的 JSON
【发布时间】:2017-06-12 00:14:29
【问题描述】:

我正在努力寻找一种将复杂数据从 webapi 服务器返回到我的客户端应用程序的方法。基本上返回的列表包含另一个对象的嵌套列表,当我查看来自服务器的响应时,我得到如下错误:

发生错误。“ObjectContent`1”类型无法序列化内容类型“application/json;”的响应正文; charset=utf-8'.System.InvalidOperationException 发生错误。检测到类型为“System.Data.Entity.DynamicProxies.Property_6238A3E9AD216212102B01343C0B28238D354B83E729485520A1EC884FE53A26”的自引用循环。 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference 的路径'propertyDataTypes[0].Properties[0].PropertyDataType.Properties'.Newtonsoft.Json.JsonSerializationException(JsonWriter writer,对象值,JsonProperty 属性,JsonContract 合同,JsonContainerContract 容器合同,JsonProperty 容器属性) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IEnumerable 值,JsonArrayContract 合同,JsonProperty 成员,JsonContainerContract 集合合同,JsonProperty containerProperty) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) 在 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,对象值,类型 objectType) 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding EffectiveEncoding) 在 System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(类型类型,对象值,流 writeStream,编码有效编码) 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(类型类型,对象值,流 writeStream,HttpContent 内容) 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(类型类型,对象值,流 writeStream,HttpContent 内容,TransportContext transportContext,CancellationToken cancelToken) --- 从先前抛出异常的位置结束堆栈跟踪 --- 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext()

这是我发送 JSON 响应的服务器代码:

    [Authorize]
    [HttpGet]
    [Route("api/propertydatatype/all")]
    public IHttpActionResult GetAllPropertyDataTypes()
    {
        var result = propertySVC.GetDataTypes().ToList();
        var config = new MapperConfiguration(cfg => {
            cfg.CreateMap<PropertyDataType, PropertyDataTypeModel>();
        });
        IMapper mapper = config.CreateMapper();
        var propertyDataTypes = mapper.Map<List<PropertyDataType>, List<PropertyDataTypeModel>>(result);
        return this.Content(HttpStatusCode.OK, new { propertyDataTypes = propertyDataTypes });
    }

【问题讨论】:

  • 错误提示检查循环引用。 PropertyDataType 或 PropertyDataTypeModel 是否具有相互指向的属性?
  • @Botonomous 是的,它们具有相同的属性,一个用作视图模型(PropertyDataTypeModel)和基于数据库结构的(PropertyDataType)。转换进行得很好。
  • 您是否要序列化实体?

标签: c# asp.net json asp.net-web-api


【解决方案1】:

错误消息表明您的数据包含无法序列化的循环引用。

您可以使用类似的方式配置循环引用的行为:

System.Web.Http.GlobalConfiguration.Configure(config =>
{
    config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 
});

您可以在此处找到有关引用循环处理的更多信息:

【讨论】:

  • 谢谢。这完成了工作!
猜你喜欢
  • 2015-01-25
  • 2017-07-22
  • 2013-05-26
  • 1970-01-01
  • 2018-08-11
  • 1970-01-01
  • 1970-01-01
  • 2016-12-29
  • 1970-01-01
相关资源
最近更新 更多