【问题标题】:WebApi OData inlinecount not workingWebApi OData 内联计数不起作用
【发布时间】:2016-01-20 11:47:26
【问题描述】:

当我在 uri 中传递 $inlinecount=allpages 时,我试图让 OData 返回数据库中的实体数。我在 Stack Overflow 中读到我应该返回 IQueryable 而不是 IHttpActionResult,但这并没有解决问题。我也尝试按照 asp.net 网站上的教程进行操作,但也没有给出任何结果。

[EnableQuery(
        PageSize = BuildingConstants.BuildingsPerPage,
        MaxTop = BuildingConstants.MaxBuildingsPerPage,
        AllowedArithmeticOperators = AllowedArithmeticOperators.None,
        AllowedLogicalOperators = AllowedLogicalOperators.None,
        AllowedFunctions = AllowedFunctions.SubstringOf,
        AllowedQueryOptions = AllowedQueryOptions.Filter | AllowedQueryOptions.OrderBy | AllowedQueryOptions.Top | AllowedQueryOptions.Skip | AllowedQueryOptions.InlineCount)]
    [ResponseType(typeof(IEnumerable<ListedBuildingResponseModel>))]
    public IQueryable<ListedBuildingResponseModel> Get()
    {
        var buildings = this.buildings
            .GetBuildings()
            .ProjectTo<ListedBuildingResponseModel>();

        return buildings;
    }

这是我写的关于 OData 的所有内容。控制器继承ApiController,而不是ODataController。在 Register 方法中,我没有添加任何 OData 路由或模型构建器。 $top$skip$orderby 和其他一切工作正常,但 $inlinecount=allpages。有关如何解决问题的任何建议?

【问题讨论】:

  • ODataV4 不是 $count=true 而不是 $inlinecount?
  • “消息”:“不支持查询参数'$count'。”
  • 将其添加到允许列表中? “AllowedQueryOptions.Count”。
  • @MarvinSmit 没有这样的查询选项“count”。即使我从属性中删除所有参数并像 [EnableQuery] 一样将其留空,我也会收到相同的错误消息。
  • 我发现我已经安装了旧的NuGet包,所以我删除了旧的并安装了OData v4包。现在,我可以使用 count=true,但我仍然没有得到响应中的实体计数。我尝试返回 IHttpActionResult 和 IQueryable,但没有任何成功。

标签: c# asp.net-web-api odata


【解决方案1】:

我设法通过添加以下代码使count=true 工作:

 public static void Register(HttpConfiguration config)
    {
        config.MapODataServiceRoute("odata", "odata", model: GetEdmModel());
    }

    private static IEdmModel GetEdmModel()
    {
        var builder = new ODataConventionModelBuilder();
        builder.EntitySet<ListedBuildingResponseModel>("Buildings");

        builder.EnableLowerCamelCase();
        var edmModel = builder.GetEdmModel();
        return edmModel;
    }

现在一切正常,除了我不能在课堂上使用我的其他操作,因为我收到 406 错误状态代码,我不知道为什么。我不希望我的其他操作支持 OData,因此我正在考虑可能的解决方案。只为 OData 操作做一个单独的控制器是一种选择,但我也有家庭、费用和债务控制器,我想支持 OData 的 Get 方法。这意味着,我需要用 1 个 Get 方法再创建 4 个控制器。有没有办法可以使用返回不同类型的方法 GetBuildingsGetHouseholdsGetExpensesGetDebts 创建单个 SearchController?因为,据我了解这行代码

uilder.EntitySet<ListedBuildingResponseModel>("Buildings");

以某种方式将BuildingsController“绑定”到ListedBuildingResponseModel

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-03
    • 2013-03-06
    • 2012-03-19
    • 1970-01-01
    • 2016-07-18
    • 2020-03-09
    • 1970-01-01
    • 2015-04-10
    相关资源
    最近更新 更多