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

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

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:

enter image description here

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?

推荐答案

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

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

https://github.com/aspnet/Mvc/issues/4160https://github.com/aspnet/EntityFramework/issues/4646讨论了这个问题,另见circular reference

Json相关问答推荐

您可以使用Jolt对JSON执行OR条件吗

JSON字符串处理注入引号

从Json响应中为需要每个值的Post请求提取多个值

ArcGIS json到Geojson的变换

Terraform迭代JSON文件以获取键值对

如何在JSONata对象中迭代并向数组添加新字段?

合并2个嵌套词典

如何对未知/变量键的字典进行编码?

根据值过滤输入的JSON并使用它准备预期的输出

如何将复杂的 JSON 反序列化为 Rust 类型?

如何从 json 中获取单个元素?

我无法在 Go - Gin 中解析日期/时间

如何限制resolveJsonModule的范围?

缺少所需的请求正文内容:org.springframework.web.method.HandlerMethod$HandlerMethodParameter

单元测试球衣 Restful Services

一起使用 Argparse 和 Json

在 JSON.stringify() 的输出中隐藏空值

Golang struct 的 XML 和 JSON 标签?

使用 Python 3 读取 JSON 文件

将 JsonArray 添加到 JsonObject