ASP.NET Web API 在 json 响应中返回空集合

本文关键字:返回 空集 集合 响应 json NET Web API ASP | 更新日期: 2023-09-27 18:36:51

好的,我在 ASP.NET Web API上遇到了问题。我按照本教程了解如何创建它。我出于我的目的对教程进行了修改。

我有一个补丁类,其中包含下载 URL 的集合。API 返回所有信息都很好,只有下载URL的集合作为null返回。但是,当我逐步完成代码调试时,它正确地返回了集合。我做错了什么。我找不到我的错误。这是模型:

public class Patch
{
    [Key]
    [Required]
    [JsonProperty("PatchVersion")]
    public string PatchVersion { get; set; }
    [Required]
    [JsonProperty("Length")]
    public long Length { get; set; }
    [JsonProperty("VersionToUpdateFrom")]
    public string VersionToUpdateFrom { get; set; }
    [JsonProperty("DownloadUris")]
    public ICollection<DownloadUri> DownloadUris { get; set; }
}
public class DownloadUri
{
    [Key]
    [Required]
    [JsonProperty("AbsolutePath")]
    public string AbsolutePath { get; set; }
    [Required]
    [JsonProperty("IndirectDownload")]
    public bool IndirectDownload { get; set; }
}

这是我的DataContext类:

public class PatchesContext : DbContext
{
    public PatchesContext()
        : base("name=PatchesContext")
    {
    }
    public DbSet<Patch> Patches { get; set; }
    public DbSet<DownloadUri> DownloadUris { get; set; }
}

这是上下文初始值设定项

public class PatchesContextInitializer : DropCreateDatabaseIfModelChanges<PatchesContext>
{
    protected override void Seed(PatchesContext context)
    {
        var downloadUri = new List<DownloadUri>
        {
            new DownloadUri
            {
                AbsolutePath = "http://downloadUri1.test", IndirectDownload = false
            },
            new DownloadUri
            {
                AbsolutePath = "http://downloadUri2.test", IndirectDownload = false
            }
        };
        var patches = new List<Patch>
        {
            new Patch
            {
                Length = 100000, PatchVersion = "1.0.0.0", VersionToUpdateFrom = string.Empty, DownloadUris = downloadUri
            },
            new Patch
            {
                DownloadUris = null, Length = 10000, PatchVersion = "1.1.0.0", VersionToUpdateFrom = "1.0.0.0"
            }
        };
        patches.ForEach(p => context.Patches.Add(p));
        context.SaveChanges();
    }
}

最后是我的控制器类。我省略了帖子,放置和删除方法,因为我还没有测试它们(如果get不起作用,则没有意义):

public class AdminController : ApiController
{
    private PatchesContext db = new PatchesContext();
    // GET api/Admin
    public IEnumerable<Patch> GetPatches()
    {
        return db.Patches.AsEnumerable();
    }
    // GET api/Admin/5
    public Patch GetPatch(string version)
    {
        Patch patch = db.Patches.Find(version);
        if (patch == null)
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
        }
        return patch;
    }
}

我希望你们都能帮助我,谢谢。

/编辑:标题如下所示:

Status Code: 200 OK
Cache-Control: no-cache
Content-Length: 291
Content-Type: application/json; charset=utf-8
Date: Fri, 25 Oct 2013 17:25:48 GMT
Expires: -1
Pragma: no-cache
Server: Microsoft-IIS/8.0
X-SourceFiles: =?UTF-8?B?RTpcVXNlcnNcUm9iZXJ0XERvY3VtZW50c1xWaXN1YWwgU3R1ZGlvIDIwMTJcUHJvamVjdHNcTjQ0V2Vic2l0ZVxONDRXZWJzaXRlXGFwaVxhZG 1pbg==?=

响应正文如下所示:

[
  {
    "$id": "1",
    "PatchVersion": "1.0.0.0",
    "Length": 100000,
    "VersionToUpdateFrom": "",
    "DownloadUris": null
  },
  {
    "$id": "2",
    "PatchVersion": "1.1.0.0",
    "Length": 10000,
    "VersionToUpdateFrom": "1.0.0.0",
    "DownloadUris": null
  }
]

ASP.NET Web API 在 json 响应中返回空集合

您没有显式加载 Patch.DownloadUris 属性,该属性也不是虚拟的,因此它也不会延迟加载它。

要么通过此行上的包含下载 Uris 显式加载它:

Patch patch = db.Patches.Find(version);

到(未经测试)

Patch patch = db.Patches.Find(version).Include("DownloadUris");

或者使该属性成为虚拟属性,并确保在上下文中启用延迟加载。

编辑注释:在 Seed 方法的代码示例中给出,只有补丁 (1.0.0.0) 具有 DownloadUri 的值,因为在补丁 (1.1.0.0) 中,您将该属性设置为 null