I'm battling to find a way to return complex data to my client application from the webapi server. Basically the returned list contains nested lists of another object, when I look at the response from the server I get errors as below:
An error has occurred.The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.System.InvalidOperationExceptionAn error has occurred.Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.Property_6238A3E9AD216212102B01343C0B28238D354B83E729485520A1EC884FE53A26'. Path 'propertyDataTypes[0].Properties[0].PropertyDataType.Properties'.Newtonsoft.Json.JsonSerializationException at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, JsonContainerContract containerContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()
This is my server code sending the JSON response:
[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 });
}