简体   繁体   English

Web API OData:如何在单个实体上扩展$?

[英]Web API OData: How do you $expand on a single entity?

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. 我已经多次阅读 两篇文章 ,以尝试找出在单个实体上使用$expand查询选项的方法,但是以我尝试过的每一种方式,我似乎都无法使这行得通。 All the other query options work and $expand also currently works on collection results. 所有其他查询选项均有效,并且$expand当前也适用于集合结果。

Model : Player entities have a navigation property called Stats in which each object contains the stats for that player for a given year. Model :玩家实体具有一个称为Stats的导航属性,其中每个对象都包含该玩家在给定年份的统计信息。

I've set up OData this way: 我已经通过以下方式设置了OData:

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?$expand=Stats

But evidently not (the result is not expanded): 但显然没有(结果没有扩展):

  • /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);
}

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. 但是,当我将其添加到控制器时, /odata/OPlayer(600)/odata/OPlayer(600)?$expand=Stats返回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? 任何人都可以提供说明或提供指针以在单个实体上支持$expand吗?

Your action GetPlayer got the parameter name incorrect. 您的操作GetPlayer的参数名称不正确。 The parameter name should be "key" instead of id. 参数名称应为“键”而不是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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM