I am having an issue with WebAPI return an empty 500.
Here’s the data classes.
public class Comment
{
public int Id { get; set; }
public string Content { get; set; }
public string Email { get; set; }
public bool IsAnonymous { get; set; }
public int ReviewId { get; set; }
public Review Review { get; set; }
}
public class Review
{
public int Id { get; set; }
public string Content { get; set; }
public int CategoryId { get; set; }
public string Topic { get; set; }
public string Email { get; set; }
public bool IsAnonymous { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
Here’s come code from the ReviewRepository.cs
public Review Get(int id)
{
return _db.Reviews.Include("Comments").SingleOrDefault(r => r.Id == id);
}
And the code from ReviewController.cs
public HttpResponseMessage Get(int id)
{
var category = _reviewRepository.Get(id);
if (category == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK, category);
}
No matter what I do, the response back from /api/reviews/1 is a 500 error. When debugging, the category is correct with all of the comments loaded.
I tried GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;, but that didn’t help. I am at a loss here!
I’m guessing it’s because you have a circular object graph, which will cause a serialization error.
http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization#handling_circular_object_references