ASP.NET Core API 只返回列表的第一个结果 [英] ASP.NET Core API only returning first result of list

查看:25
本文介绍了ASP.NET Core API 只返回列表的第一个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个团队 web api 控制器并尝试调用 GET 方法来获取数据库中所有团队的 json 结果.但是,当我拨打电话时,我只在 json 中返回了第一支球队,但是当我在 return 语句上设置断点时,它包含所有 254 支球队以及所有比赛.

这是我正在处理的两个模型:

公开课团队{公共字符串 ID { 获取;放;}公共字符串名称 { 获取;放;}公共字符串图标 { 获取;放;}公共字符串吉祥物{得到;放;}公共字符串会议{得到;放;}公共 int NationalRank { 得到;放;}公开列表<游戏>游戏{得到;放;}}公开课游戏{公共字符串 ID { 获取;放;}公共字符串对手{得到;放;}公共字符串 OpponentLogo { 获取;放;}公共字符串 GameDate { 获取;放;}公共字符串 GameTime { 获取;放;}公共字符串 TvNetwork { 获取;放;}公共字符串 TeamId { 获取;放;}公共团队团队{得到;放;}}

当我这样做时:

[HttpGet]公共异步任务<列表<团队>>得到(){var 团队 = 等待 _context.Teams.ToListAsync();归队;}

我获得了所有 254 个团队,但 Game 属性为 null,因为 EF Core 不支持延迟加载.所以我真正想做的是像这样添加 .Include() :

[HttpGet]公共异步任务<列表<团队>>得到(){var 团队 = 等待 _context.Teams.Include(t => t.Games).ToListAsync();归队;}

这将返回第一场比赛的第一支球队,但没有其他任何事情.这是json:

<预><代码>[{"id": "007b4f09-d4da-4040-be3a-8e45fc0a572b","name": "新墨西哥","icon": "lobos.jpg","mascot": "新墨西哥罗伯斯","会议": "MW - 山","nationalRank": null,游戏":[{"id": "1198e6b1-e8ab-48ab-a63f-e86421126361",对手":vs 空军*","opponentLogo": "falcons.jpg","gameDate": "10 月 15 日星期六","gameTime": "待定",tvNetwork":空,"teamId": "007b4f09-d4da-4040-be3a-8e45fc0a572b"}]}]

当我在 return 语句上设置断点时,它显示有 254 支球队,每支球队都正确填充了他们的比赛......但 json 结果没有反映.这是一张图片:

我尝试同步和异步执行此操作,但得到相同的结果.你知道为什么我在 json 中只得到一个结果,但在断点处它有所有的结果吗?

解决方案

将此添加到 Startup.cs 中的 public void ConfigureServices(IServiceCollection services) 方法:>

services.AddMvc().AddJsonOptions(options => {options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;});

讨论了这个问题 https://github.com/aspnet/Mvc/issues/4160https://github.com/aspnet/EntityFramework/issues/4646 另见循环引用

I have created a teams web api controller and trying to call the GET method to get the json result of all the teams in the database. But when I make the call I am only getting the first team back in the json but when I set a breakpoint on the return statement it has all 254 teams along with all of the games.

These are the two models that I am dealing with:

public class Team
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string Icon { get; set; }
    public string Mascot { get; set; }
    public string Conference { get; set; }
    public int NationalRank { get; set; }

    public List<Game> Games { get; set; }
}

public class Game
{
    public string Id { get; set; }
    public string Opponent { get; set; }
    public string OpponentLogo { get; set; }
    public string GameDate { get; set; }
    public string GameTime { get; set; }
    public string TvNetwork { get; set; }
    public string TeamId { get; set; }

    public Team Team { get; set; }
}

When I do this:

[HttpGet]
public async Task<List<Team>> Get()
{
    var teams = await _context.Teams.ToListAsync();

    return teams;
}

I get all 254 teams but Game property is null because EF Core does not support lazy loading. So what I really want to do is add the .Include() like this:

[HttpGet]
public async Task<List<Team>> Get()
{
    var teams = await _context.Teams.Include(t => t.Games).ToListAsync();

    return teams;
}

This returns the first team with the first game but nothing else. Here is the json:

[
  {
    "id": "007b4f09-d4da-4040-be3a-8e45fc0a572b",
    "name": "New Mexico",
    "icon": "lobos.jpg",
    "mascot": "New Mexico Lobos",
    "conference": "MW - Mountain",
    "nationalRank": null,
    "games": [
      {
        "id": "1198e6b1-e8ab-48ab-a63f-e86421126361",
        "opponent": "vs Air Force*",
        "opponentLogo": "falcons.jpg",
        "gameDate": "Sat, Oct 15",
        "gameTime": "TBD ",
        "tvNetwork": null,
        "teamId": "007b4f09-d4da-4040-be3a-8e45fc0a572b"
      }
    ]
  }
]

When I set a break point on the return statement it shows that there are 254 teams and every team has their games populated correctly...but the json result does not reflect. Here is an image:

I have tried doing this both synchronously and asynchronously but getting the same result. Do you know why I am only getting one result back in the json but at the breakpoint it has all of the results?

解决方案

Add this to Startup.cs inside the public void ConfigureServices(IServiceCollection services) method:

services.AddMvc().AddJsonOptions(options => {
            options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
        });

The issue was discussed https://github.com/aspnet/Mvc/issues/4160 and https://github.com/aspnet/EntityFramework/issues/4646 also see circular reference

这篇关于ASP.NET Core API 只返回列表的第一个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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