自引用循环检测 - 从获取的WebAPI回数据到浏览器 [英] Self referencing loop detected - Getting back data from WebApi to the browser

查看:272
本文介绍了自引用循环检测 - 从获取的WebAPI回数据到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架,并具有让父母与子女数据到浏览器的一个问题。这里是我的类:

 公共类问题
 {
    公众诠释QuestionId {搞定;组; }
    公共字符串名称{搞定;组; }
    公共虚拟的ICollection<应答和GT;答案{搞定;组; }
}公共类答
{
    公众诠释AnswerId {搞定;组; }
    公共字符串文本{搞定;组; }
    公众诠释QuestionId {搞定;组; }
    公共虚拟问题问题{搞定;组; }
}

我用下面的code归还问题和答案数据:

 公开的IList<问题> GetQuestions(INT subTopicId,诠释questionStatusId)
    {
        VAR问题= _questionsRepository.GetAll()
            。凡(一个= GT; a.SubTopicId == subTopicId&放大器;&放大器;
                   (questionStatusId == || 99
                    a.QuestionStatusId == questionStatusId))
            .INCLUDE(一个= GT; a.Answers)
            .ToList();
        返回的问题;
    }

在C#的一面,这似乎不过工作,我发现答案对象都引用回的问题。当我使用的WebAPI获取数据到浏览器,我得到了以下信息:


  

在'ObjectContent`1类型没有序列化内容类型应用程序/ JSON响应主体;字符集= UTF-8。


  
  

用型Models.Core.Question'财产'问题'检测自参考循环


这是因为该问题有答案和答案有一个参考回的问题?我看过的所有地方建议在有孩子家长一个参考,所以我不知道该怎么做。有人可以给我一些建议这一点。


解决方案

  

这是因为该问题有答案和解答有
  参照回的问题?


是的。它不能被序列化。

编辑:见Tallmaris的回答和奥多的评论,因为它更简单,可以设置全局

。<$p$p><$c$c>GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Re‌​ferenceLoopHandling = ReferenceLoopHandling.Ignore;

老答案:

该项目EF对象问题来自己的中级或DataTransferObject。这DTO可以被成功序列化。

 公共类QuestionDto
{
    公共QuestionDto()
    {
        this.Answers =新的List&LT;应答和GT;();
    }
    公众诠释QuestionId {搞定;组; }
    ...
    ...
    公共字符串名称{搞定;组; }
    公开名单&LT;应答和GT;答案{搞定;组; }
}

是这样的:

 公开的IList&LT; QuestionDto&GT; GetQuestions(INT subTopicId,诠释questionStatusId)
{
    VAR问题= _questionsRepository.GetAll()
        。凡(一个= GT; a.SubTopicId == subTopicId&放大器;&放大器;
               (questionStatusId == || 99
                a.QuestionStatusId == questionStatusId))
        .INCLUDE(一个= GT; a.Answers)
        .ToList();    VAR DTO = questions.Select(X =&gt;新建QuestionDto {标题= x.Title ...});    返回DTO;
}

I am using Entity Framework and having a problem with getting parent and child data to the browser. Here are my classes:

 public class Question
 {
    public int QuestionId { get; set; }
    public string Title { get; set; }
    public virtual ICollection<Answer> Answers { get; set; }
}

public class Answer
{
    public int AnswerId { get; set; }
    public string Text { get; set; }
    public int QuestionId { get; set; }
    public virtual Question Question { get; set; }
}

I am using the following code to return the question and answer data:

    public IList<Question> GetQuestions(int subTopicId, int questionStatusId)
    {
        var questions = _questionsRepository.GetAll()
            .Where(a => a.SubTopicId == subTopicId &&
                   (questionStatusId == 99 ||
                    a.QuestionStatusId == questionStatusId))
            .Include(a => a.Answers)
            .ToList();
        return questions; 
    }

On the C# side this seems to work however I notice that the answer objects have references back to the question. When I use the WebAPI to get the data to the browser I get the following message:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.

Self referencing loop detected for property 'question' with type 'Models.Core.Question'.

Is this because the Question has Answers and the Answers have a reference back to Question? All the places I have looked suggest having a reference to the parent in the child so I am not sure what to do. Can someone give me some advice on this.

解决方案

Is this because the Question has Answers and the Answers have a reference back to Question?

Yes. It cannot be serialized.

EDIT: See Tallmaris's answer and OttO's comment as it is simpler and can be set globally.

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Re‌​ferenceLoopHandling = ReferenceLoopHandling.Ignore;

Old Answer:

Project the EF object Question to your own intermediate or DataTransferObject. This Dto can then be serialized successfully.

public class QuestionDto
{
    public QuestionDto()
    {
        this.Answers = new List<Answer>();
    } 
    public int QuestionId { get; set; }
    ...
    ...
    public string Title { get; set; }
    public List<Answer> Answers { get; set; }
}

Something like:

public IList<QuestionDto> GetQuestions(int subTopicId, int questionStatusId)
{
    var questions = _questionsRepository.GetAll()
        .Where(a => a.SubTopicId == subTopicId &&
               (questionStatusId == 99 ||
                a.QuestionStatusId == questionStatusId))
        .Include(a => a.Answers)
        .ToList();

    var dto = questions.Select(x => new QuestionDto { Title = x.Title ... } );

    return dto; 
}

这篇关于自引用循环检测 - 从获取的WebAPI回数据到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆