简体   繁体   中英

Web API OData inlinecount not mapped

My question is similar to: 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.

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

// enables attribute routing
config.MapHttpAttributeRoutes();

The method GetAllProducts of the ProductController:

[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

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:

在此输入图像描述

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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