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

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

问题描述

我已经创建了一个团队web api控制器,并尝试调用GET方法来获取数据库中所有团队的json结果。但是当我打电话时,我只是把第一个团队带回了json,但是当我在return语句中设置了一个断点,所有的254个团队以及所有的游戏。



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

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

public List< Game>游戏{get;组; }
}

public class Game
{
public string Id {get;组; }
public string对手{get;组; }
public string OpponentLogo {get;组; }
public string GameDate {get;组; }
public string GameTime {get;组; }
public string TvNetwork {get;组; }
public string TeamId {get;组; }

public Team Team {get;组; }
}

当我这样做:

  [HttpGet] 
public async任务<列表<团队>> Get()
{
var teams = await _context.Teams.ToListAsync();

返回队伍;
}

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

  [HttpGet] 
public async任务<列表与LT;团队及GT;> Get()
{
var teams = await _context.Teams.Include(t => t.Games).ToListAsync();

返回队伍;
}

这将返回第一个游戏,但没有其他的游戏。这是json:

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

当我在return语句上设置一个断点时显示有254支队伍,每支球队的游戏人数正确...但是json的结果并没有反映出来。以下是图片:





我已经尝试同步和异步但得到相同的结果。你知道为什么我只能在json中得到一个结果,但在断点上它有所有的结果?

解决方案

尝试这样:

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

该问题已讨论 https://github.com/aspnet/Mvc/issues/4160 https://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?

解决方案

Try this:

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天全站免登陆