ASP.NET Core中有关GET请求的不完整JSON数据 [英] Incomplete JSON data on GET request in ASP.NET Core

查看:529
本文介绍了ASP.NET Core中有关GET请求的不完整JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能通过链接从基准站获取所有数据 https://localhost:XXXXX/api/comments (获取请求) 更新后,页面数据不再显示..

Why I get not all data from base by link https://localhost:XXXXX/api/comments (GET request) After update page data no longer appears ..

回复: [{"id":1,"text":"Comment1","userId":1,"parentCommentId":null,"user":null,"parentComment":null,"childrenComments":null},{"id :2," text:" Comment2," userId:1," parentCommentId:1," user:null," parentComment:{" id:1," text:" Comment1," userId:1," parentCommentId:null," user:null," parentComment:null," childrenComments:[

Responce: [{"id":1,"text":"Comment1","userId":1,"parentCommentId":null,"user":null,"parentComment":null,"childrenComments":null},{"id":2,"text":"Comment2","userId":1,"parentCommentId":1,"user":null,"parentComment":{"id":1,"text":"Comment1","userId":1,"parentCommentId":null,"user":null,"parentComment":null,"childrenComments":[

不加载从属项目.. 我在做什么错了?

Does not load subordinate item .. What am I doing wrong?

// GET: api/Comments
[HttpGet]
public IEnumerable<Comment> GetComments()
{
    return _context.Comments;
}

推荐答案

您还必须加载关系.做到这一点的两种主要方法是通过Include预先加载或延迟加载.但是,通常应避免延迟加载,尤其是在这种情况下.序列化对象时,最终可能会由于延迟加载而无意间发出了数百甚至数千个查询.

You must load the relationships as well. The two primary ways of doing that are eager-loading via Include or lazy-loading. However, lazy-loading should be avoided in general, and especially so in cases like this. When you're serializing an object, you could end up issuing hundreds or even thousands of queries inadvertently with lazy-loading.

简而言之,为您关心的关系添加Include子句:

Long and short, add Include clauses for the relationships you care about:

return _context.Comments
    .Include(x => x.User)
    .Include(x => x.parentComment)
    .Include(x => x.childrenComments);

如果需要更大的灵活性,可以使用OData或GraphQL.这两种方法都可以使客户选择性地包括他们想要/需要的关系,这意味着您不必每次都需要加入所有内容.

If you want more flexibility, you can employ either OData or GraphQL. Either will allow the client to selectively include the relationships they want/need, meaning you won't necessarily need to join everything every time.

这篇关于ASP.NET Core中有关GET请求的不完整JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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