加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – WebAPI Empty 500错误

发布时间:2020-12-16 06:25:02 所属栏目:asp.Net 来源:网络整理
导读:我遇到了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; } pub
我遇到了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;,但这没有帮助.我在这里不知所措!

解决方法

我猜它是因为你有一个圆形对象图,这将导致序列化错误.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读