Reputation: 8189
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!
Upvotes: 4
Views: 2489
Reputation: 6622
I'm guessing it's because you have a circular object graph, which will cause a serialization error.
Upvotes: 5
Reputation: 20617
It is probably the serialization of ICollection<Comment> Comments
or Category Category
.
Upvotes: 0
Reputation: 101
I was running into the same issue. In addition to the GlobalConfiguration policy, you may need to include the following in your web.config.
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
Upvotes: 0