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

查看:276
本文介绍了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.

Messageboard型号:

public class MessageBoard
    {
        public long Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public ICollection<Message> Messages { get; set; }
    }

Message型号:

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的一部分.这是在邮递员上访问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,名称":测试板2",描述":第二个留言板 测试目的.,"消息:[{" id:1,"文本:"发布我的第一个 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.

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

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中的相关数据和序列化

For more details: Related data and serialization in EF Core

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

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