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

时间:2013-10-25 16:05:10

标签: c# asp.net json asp.net-web-api linq-to-entities

好的,我的ASP.NET Web API存在问题。我按照this教程了解了如何创建它。为了我的目的,我修改了教程。

我有一个补丁类,它有一个下载URL集合。 API正在返回每个信息,只有DownloadURLs的集合返回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; }
}

这是ContextInitializer

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();
    }
}

最后这是我的控制器类。我遗漏了Post,Put和Delete方法,因为我还没有测试它们(如果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
  }
]

1 个答案:

答案 0 :(得分:0)

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

在此行上通过Include for DownloadUris显式加载:

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

到(未经测试)

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

或者将该属性设置为虚拟,并确保在您的上下文中启用延迟加载。

编辑说明: 在Seed方法的代码示例中给出,只有Patch(1.0.0.0)将具有DownloadUris的值,因为在Patch(1.1.0.0)中您将该属性设置为null