网页API的OData:你如何在$一个单一的实体扩张? [英] Web API OData: How do you $expand on a single entity?

查看:97
本文介绍了网页API的OData:你如何在$一个单一的实体扩张?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过这两个这些 <一href=\"http://blogs.msdn.com/b/webdev/archive/2013/07/05/introducing-select-and-expand-support-in-web-api-odata.aspx\"相对=nofollow>文章的多次尝试,并想出一个办法使用 $展开一个单一的实体查询选项,但在每一个方法我试过,我似乎没有能够使它发挥作用。所有其它工作选项的查询和 $展开也是目前从事集合结果。

I've read both these articles multiple times to try and figure out a way to use the $expand query option on a single entity, but in each and every way I've tried, I just can't seem to be able to make it work. All the other query options work and $expand also currently works on collection results.

型号:玩家的实体有一个叫做统计导航属性,其中每个对象包含了玩家对于给定的年度统计数据。

Model: Player entities have a navigation property called Stats in which each object contains the stats for that player for a given year.

我已经设置了OData的是这样的:

I've set up OData this way:

config.EnableQuerySupport();

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Player>("OPlayer");
modelBuilder.EntitySet<PlayerStatistics>("OPlayerStats");

Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel();
config.Routes.MapODataRoute("ODataRoute", "odata", model);

本来,我已经建立了我的控制器是这样的:

Originally, I had set up my controller this way:

public class OPlayerController : EntitySetController<Player, int>
{
    private readonly DatabaseContext _db = new DatabaseContext();

    protected override Player GetEntityByKey(int key)
    {
        return _db.Players.FirstOrDefault(p => p.PlayerId == key);
    }

    public override IQueryable<Player> Get()
    {
        return _db.Players.AsQueryable();
    }

    protected override void Dispose(bool disposing)
    {
        _db.Dispose();
        base.Dispose(disposing);
    }
}

通过这样的结构,我可以使这些查询:

With this configuration, I can make these queries:


  • / ODATA / OPLAYER(600)

  • / ODATA / OPLAYER

  • / ODATA / OPLAYER?$ =拓展统计信息

  • /odata/OPlayer(600)
  • /odata/OPlayer
  • /odata/OPlayer?$expand=Stats

但显然不是(结果没有展开):

But evidently not (the result is not expanded):


  • / ODATA / OPLAYER(600)?$ =拓展统计信息

  • /odata/OPlayer(600)?$expand=Stats

两篇文章都提到,为了支持它,你必须添加一个方法(行动?)到你的控制器,看起来像这样:

Both articles mention that in order to support it, you have to add a method (action?) to your controller that looks like this:

[Queryable]
public SingleResult<Player> GetPlayer(int id)
{
    return SingleResult.Create(_dbContext.Players.Where(c => c.ID == id);
}

当我将它添加到我的控制器,虽然,这两种 / ODATA / OPLAYER(600) / ODATA / OPLAYER(600)?$扩大=统计收益没有采取任何控制器OPLAYER的要求相匹配。上找到

When I add this to my controller though, both /odata/OPlayer(600) and /odata/OPlayer(600)?$expand=Stats return No action was found on the controller 'OPlayer' that matches the request.

任何人都可以作出澄清,或以支持 $展开在一个单一的实体指点?

Can anyone provide clarifications, or give pointers in order to support $expand on a single entity?

推荐答案

您的行动 GetPlayer 得到了参数名称不正确。参数名称应该是关键,而不是ID。试试这个作为你的行动,而不是,

Your action GetPlayer got the parameter name incorrect. The parameter name should be "key" instead of id. Try this as your action instead,

[Queryable]
public SingleResult<Player> GetPlayer([FromODataUri]int key)
{
    return SingleResult.Create(_db.Players.Where(c => c.PlayerId == key));
}

这篇关于网页API的OData:你如何在$一个单一的实体扩张?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆