简体   繁体   English

Web API OData inlinecount未映射

[英]Web API OData inlinecount not mapped

My question is similar to: web-api-odata-inlinecount-not-working 我的问题类似于: web-api-odata-inlinecount-not-working

I have installed the following packages: 我安装了以下软件包:

<packages>
  <package id="Microsoft.AspNet.Cors" version="5.0.0-rc1" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.0.0-rc1" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.0.0-rc1" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Cors" version="5.0.0-rc1" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.OData" version="5.0.0-rc1" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.SelfHost" version="5.0.0-rc1" targetFramework="net45" />
  <package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net45" />
  <package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="5.0.6" targetFramework="net45" />
  <package id="System.Spatial" version="5.6.0" targetFramework="net45" />
</packages>

The api is selfhosted with cors and attribute routing enabled. api是自动主机,启用了cors和属性路由。

// used for development purpose only
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);

// enables attribute routing
config.MapHttpAttributeRoutes();

The method GetAllProducts of the ProductController: ProductController的方法GetAllProducts:

[Queryable]
[HttpGet("products")]
public PageResult<ProductViewModel> GetAllProducts(ODataQueryOptions<ProductViewModel> options)
{
    //return products.AsQueryable();
    ODataQuerySettings settings = new ODataQuerySettings()
    {
        PageSize = 2
    };

    IQueryable results = options.ApplyTo(products.AsQueryable(), settings);

    Uri uri = Request.GetNextPageLink();
    long? inlineCount = Request.GetInlineCount();

    PageResult<ProductViewModel> response = new PageResult<ProductViewModel>(
        results as IEnumerable<ProductViewModel>,
        uri,
        inlineCount);

    return response;
}

The output by querying 通过查询输出

http://localhost/api/products 

is as follows: 如下:

HTTP://本地主机/ API /产品

If I'm appending ?$inlinecount=allpages the output by querying 如果我要附加?$ inlinecount = allpages输出查询

http://localhost/api/products?$inlinecount=allpages

is as follows: 如下:

HTTP://本地主机/ API /产品$ inlinecount =所有页

During debugging the uri and count are properly set but not mapped in the json response: 在调试期间,uri和count已正确设置但未在json响应中映射:

在此输入图像描述

What I'm missing? 我错过了什么?

I found my mistake. 我发现了自己的错误。 By removing the attribute [Queryable] it works just fine. 通过删除属性[可查询]它可以正常工作。

[HttpGet("products")]
public PageResult<ProductViewModel> GetAllProducts(ODataQueryOptions<ProductViewModel> options)

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

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