<legend id='qBDTi'><style id='qBDTi'><dir id='qBDTi'><q id='qBDTi'></q></dir></style></legend>

    1. <tfoot id='qBDTi'></tfoot>
    2. <small id='qBDTi'></small><noframes id='qBDTi'>

      1. <i id='qBDTi'><tr id='qBDTi'><dt id='qBDTi'><q id='qBDTi'><span id='qBDTi'><b id='qBDTi'><form id='qBDTi'><ins id='qBDTi'></ins><ul id='qBDTi'></ul><sub id='qBDTi'></sub></form><legend id='qBDTi'></legend><bdo id='qBDTi'><pre id='qBDTi'><center id='qBDTi'></center></pre></bdo></b><th id='qBDTi'></th></span></q></dt></tr></i><div id='qBDTi'><tfoot id='qBDTi'></tfoot><dl id='qBDTi'><fieldset id='qBDTi'></fieldset></dl></div>
          <bdo id='qBDTi'></bdo><ul id='qBDTi'></ul>

        检测到自引用循环 - 从 WebApi 取回数据到浏览器

        时间:2023-08-24
          <bdo id='OySug'></bdo><ul id='OySug'></ul>

          <small id='OySug'></small><noframes id='OySug'>

          <tfoot id='OySug'></tfoot>
        • <legend id='OySug'><style id='OySug'><dir id='OySug'><q id='OySug'></q></dir></style></legend>
                <i id='OySug'><tr id='OySug'><dt id='OySug'><q id='OySug'><span id='OySug'><b id='OySug'><form id='OySug'><ins id='OySug'></ins><ul id='OySug'></ul><sub id='OySug'></sub></form><legend id='OySug'></legend><bdo id='OySug'><pre id='OySug'><center id='OySug'></center></pre></bdo></b><th id='OySug'></th></span></q></dt></tr></i><div id='OySug'><tfoot id='OySug'></tfoot><dl id='OySug'><fieldset id='OySug'></fieldset></dl></div>

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

                  问题描述

                  我正在使用 Entity Framework,但在将父子数据传输到浏览器时遇到问题.这是我的课程:

                  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; 
                      }
                  

                  在 C# 方面,这似乎可行,但我注意到答案对象有对问题的引用.当我使用 WebAPI 将数据获取到浏览器时,我收到以下消息:

                  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:

                  ObjectContent`1"类型未能序列化内容类型application/json;"的响应正文;charset=utf-8'.

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

                  检测到类型为Models.Core.Question"的属性question"的自引用循环.

                  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?

                  是的.无法序列化.

                  请参阅 Tallmaris 的答案和 OttO 的评论,因为它更简单并且可以全局设置.

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

                  旧答案:

                  将 EF 对象 Question 投影到您自己的中间对象或 DataTransferObject.然后这个Dto就可以序列化成功了.

                  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; }
                  }
                  

                  类似:

                  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 取回数据到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:JContainer、JObject、JToken和Linq混淆 下一篇:如何从 Json.NET 获取密钥列表?

                  相关文章

                  最新文章

                    <tfoot id='tIG8b'></tfoot>

                    <i id='tIG8b'><tr id='tIG8b'><dt id='tIG8b'><q id='tIG8b'><span id='tIG8b'><b id='tIG8b'><form id='tIG8b'><ins id='tIG8b'></ins><ul id='tIG8b'></ul><sub id='tIG8b'></sub></form><legend id='tIG8b'></legend><bdo id='tIG8b'><pre id='tIG8b'><center id='tIG8b'></center></pre></bdo></b><th id='tIG8b'></th></span></q></dt></tr></i><div id='tIG8b'><tfoot id='tIG8b'></tfoot><dl id='tIG8b'><fieldset id='tIG8b'></fieldset></dl></div>

                    <small id='tIG8b'></small><noframes id='tIG8b'>

                    <legend id='tIG8b'><style id='tIG8b'><dir id='tIG8b'><q id='tIG8b'></q></dir></style></legend>
                    • <bdo id='tIG8b'></bdo><ul id='tIG8b'></ul>