Asp.Net Core Web API 2.2 控制器不返回完整的 JSON [英] Asp.Net Core Web API 2.2 Controller not returning complete JSON

查看:18
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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