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

      <bdo id='y8YcW'></bdo><ul id='y8YcW'></ul>

      <tfoot id='y8YcW'></tfoot>

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

      1. <legend id='y8YcW'><style id='y8YcW'><dir id='y8YcW'><q id='y8YcW'></q></dir></style></legend>

      2. Asp.Net Core Web API 2.2 控制器不返回完整的 JSON

        时间:2023-07-11

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

          <small id='1400Q'></small><noframes id='1400Q'>

          <legend id='1400Q'><style id='1400Q'><dir id='1400Q'><q id='1400Q'></q></dir></style></legend>
            • <bdo id='1400Q'></bdo><ul id='1400Q'></ul>
                <tfoot id='1400Q'></tfoot>

                  本文介绍了Asp.Net Core Web API 2.2 控制器不返回完整的 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的 Asp.Net Core Web API 2.2 项目中有一个 Web API 控制器.

                  I have a Web API Controller in my Asp.Net Core Web API 2.2 project.

                  留言板型号:

                  public class MessageBoard
                      {
                          public long Id { get; set; }
                          public string Name { get; set; }
                          public string Description { get; set; }
                  
                          public ICollection<Message> Messages { get; set; }
                      }
                  

                  消息模型:

                  public class Message
                      {
                          public long Id { get; set; }
                          public string Text { get; set; }
                          public string User { get; set; }
                          public DateTime PostedDate { get; set; }
                  
                          public long MessageBoardId { get; set; }
                          [ForeignKey("MessageBoardId")]
                          public MessageBoard MessageBoard { get; set; }
                      }
                  

                  这是我的 Web API 控制器操作之一,为简洁起见:

                  This is one of my Web API Controller actions, shortened for brevity:

                  [Route("api/[controller]")]
                  [ApiController]
                  public class MessageBoardsController : ControllerBase
                  {        
                        // GET: api/MessageBoards
                        [HttpGet]
                        public async Task<ActionResult<IEnumerable<MessageBoard>>> GetMessageBoards()
                        {
                           return await _context.MessageBoards
                              .Include(i => i.Messages)
                              .ToListAsync();
                        }
                  }
                  

                  每当我向 MessageBoards 发出 GET 请求时,只会返回正确 JSON 的一部分.这是在 Postman 上访问 https://localhost:44384/api/MessageBoards/ 返回的 JSON:

                  Whenever I issue a GET request to MessageBoards, only part of the correct JSON is returned. Here is the returned JSON from accessing https://localhost:44384/api/MessageBoards/ on Postman:

                  [{"id":1,"name":"Test Board 2","description":"第二个留言板测试目的.","messages":[{"id":1,"text":"发布我的第一个message!","user":"Jesse","postedDate":"2019-01-01T00:00:00","messageBoardId":1

                  [{"id":1,"name":"Test Board 2","description":"A 2nd Message board for testing purposes.","messages":[{"id":1,"text":"Posting my first message!","user":"Jesse","postedDate":"2019-01-01T00:00:00","messageBoardId":1

                  JSON 被截断(因此它是一个丑陋的块并且没有被 Postman 美化),大概是由于 Message 模型上的 MessageBoard 属性,因为它是第一个丢失的 JSON 项.

                  The JSON is cut-off (hence why it's an ugly block and not beautified by Postman), presumably due to the MessageBoard property on the Message model since it is the first missing JSON item.

                  如何使操作正确返回 MessageBoard 和子消息列表?

                  How can I make the action correctly return the list of MessageBoards and child Messages?

                  推荐答案

                  我看到您在查询中使用了 Eager Loading.因此,在您的 Startup 类中添加以下配置以忽略它在对象图中找到的循环并正确生成 JSON 响应.

                  I see you are using Eager Loading in your query. So add the following configuration in your Startup class to ignore cycles that it finds in the object graph and to generate JSON response properly.

                  public void ConfigureServices(IServiceCollection services)
                  {
                      ...
                  
                      services.AddMvc()
                          .AddJsonOptions(
                              options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                          );
                  
                      ...
                  }
                  

                  更多详情:EF Core 中的相关数据和序列化

                  这篇关于Asp.Net Core Web API 2.2 控制器不返回完整的 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:HttpRuntime.Cache 等效于 asp.net 5、MVC 6 下一篇:从 Startup 类访问 Configuration 对象

                  相关文章

                    <legend id='2kIgV'><style id='2kIgV'><dir id='2kIgV'><q id='2kIgV'></q></dir></style></legend>
                    <tfoot id='2kIgV'></tfoot>

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

                        <bdo id='2kIgV'></bdo><ul id='2kIgV'></ul>