jcreamer898
jcreamer898

Reputation: 8189

WebAPI Empty 500 Error

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

Answers (3)

Mike Wasson
Mike Wasson

Reputation: 6622

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

Upvotes: 5

rick schott
rick schott

Reputation: 20617

It is probably the serialization of ICollection<Comment> Comments or Category Category.

Upvotes: 0

reverentgeek
reverentgeek

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

Related Questions