我遇到了 WebAPI 返回空 500 的问题。
这是数据类。
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; }
}
这是来自 ReviewRepository.cs 的代码
public Review Get(int id)
{
return _db.Reviews.Include("Comments").SingleOrDefault(r => r.Id == id);
}
以及来自 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);
}
无论我做什么,从 /api/reviews/1 返回的响应都是 500 错误。调试时,类别正确,所有评论都已加载。
我试过GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
了,但这没有帮助。我在这里不知所措!