Web API OData inlinecount未映射

时间:2013-08-25 12:03:08

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

我的问题类似于:web-api-odata-inlinecount-not-working

我安装了以下软件包:

<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>

api是自动托管的,并且启用了cors和属性路由。

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

// enables attribute routing
config.MapHttpAttributeRoutes();

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;
}

通过查询输出

http://localhost/api/products 

如下:

http://localhost/api/products

如果我要追加?$ inlinecount = allpages输出查询

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

如下:

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

在调试期间,uri和count已正确设置但未映射到json响应中:

enter image description here

我缺少什么?

1 个答案:

答案 0 :(得分:9)

我发现了自己的错误。通过删除属性[可查询],它可以正常工作。

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