9

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://localhost/api/products

If I'm appending ?$inlinecount=allpages the output by querying

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

is as follows:

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

During debugging the uri and count are properly set but not mapped in the json response:

enter image description here

What I'm missing?

4

1 回答 1

10

I found my mistake. By removing the attribute [Queryable] it works just fine.

[HttpGet("products")]
public PageResult<ProductViewModel> GetAllProducts(ODataQueryOptions<ProductViewModel> options)
于 2013-08-25T14:53:52.877 回答