【问题标题】:WebAPI Empty 500 ErrorWebAPI 空 500 错误
【发布时间】:2012-06-27 03:54:34
【问题描述】:

我遇到了 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 错误。调试时,加载所有 cmets 时类别正确。

我试过GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;,但没有帮助。我在这里不知所措!

【问题讨论】:

    标签: asp.net asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    【讨论】:

    • 这肯定有帮助。 “Reviewed.Models.Comment”类型的对象图包含循环,如果禁用引用跟踪,则无法序列化。
    【解决方案2】:

    我遇到了同样的问题。除了 GlobalConfiguration 政策,您可能需要在 web.config 中包含以下内容。

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

    【讨论】:

    • 只有一个空的 500 仍然没有任何结果。:(
    【解决方案3】:

    大概是ICollection&lt;Comment&gt; Comments或者Category Category的序列化。

    【讨论】:

      猜你喜欢
      • 2017-12-17
      • 1970-01-01
      • 2022-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多