为什么生成的JSON被截断?

时间:2019-01-15 14:48:35

标签: json asp.net-core-webapi

我有一个使用.NET Core构建的WebAPI。

API返回具有景点的城市列表。 (一对多关系)

我正在使用Postman检查结果,但是在浏览器中完全一样。

发生了什么(与代码本身无关),当我想让城市没有景点时,返回的JSON看起来很好并且格式正确:

https://localhost:44316/api/cities?includeAttractions=false

但是,当我想让城市具有景点时,返回的JSON被截断并且无法格式化:

https://localhost:44316/api/cities?includeAttractions=true

请告知为什么会被截断?

WebAPI returns proper valid collection

型号:

public class City
{
    public City()
    {
        Attractions = new HashSet<Attraction>();
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int Population { get; set; }
    public int NumberOfAttractions { get { return Attractions.Count(); } }
    public virtual ICollection<Attraction> Attractions { get; set; }
}

public class Attraction
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int CityId { get; set; }
    public virtual City City { get; set; }
}

public class CityInfoContext : DbContext
{
    public CityInfoContext(DbContextOptions options) : base(options)
    {
    }

    public virtual DbSet<City> Cities { get; set; }
    public virtual DbSet<Attraction> Attractions { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Attraction>(entity =>
        {
            entity.HasIndex(e => e.CityId);
            entity.Property(e => e.Description).IsRequired().HasMaxLength(200);
            entity.Property(e => e.Name).IsRequired().HasMaxLength(50);
            entity.HasOne(d => d.City).WithMany(p => p.Attractions).HasForeignKey(d => d.CityId);
        });

        modelBuilder.Entity<City>(entity =>
        {
            entity.Property(e => e.Description).IsRequired().HasMaxLength(200);
            entity.Property(e => e.Name).IsRequired().HasMaxLength(50);
        });
    }
}

0 个答案:

没有答案