WebAPI Error Empty 500

I have a problem with returning a WebAPI to an empty 500.

Here are 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 is the code from 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 answer from / api / reviews / 1 is a 500 error. When debugging, the category matches all the comments loaded.

I tried GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; , but it did not help. I'm here in difficulty!

+4
source share
3 answers

I assume, because you have a pie chart of objects, which will result in a serialization error.

http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization#handling_circular_object_references

+5
source

I ran into the same problem. In addition to the GlobalConfiguration policy, you may need to include the following in your web.config.

 <system.webServer> <httpErrors existingResponse="PassThrough" /> </system.webServer> 
0
source

This is probably a serialization of ICollection<Comment> Comments or Category Category .

0
source

All Articles