Asp.Net Core Web API 2.2 Controller not returning complete JSON(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Asp.Net Core Web API 2.2 控制器不返回完整的 JSON
基础教程推荐
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01