网页API的OData Inlinecount不工作 [英] Web API OData Inlinecount not working

查看:221
本文介绍了网页API的OData Inlinecount不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的开箱Values​​Controller的一个的ASP.NET Web API应用

I am using the out of the box ValuesController in a ASP.NET Web API application

 public class ValuesController : ApiController
 {
     // GET api/values
     [Queryable(PageSize = 1)]
     public IQueryable<string> Get()
     {
         return new string[] { "value1", "value2", "value3", "value4", "value5" }.AsQueryable();
     }
 }

当我 GET HTTP://本地主机/ API /值$ inlinecount =所有页

这是响应

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>value1</string>
</ArrayOfString>

我都注释掉 config.EnableQuerySupport();

筛选,排序做工精细。

Filtering, sorting work fine.

如果我尝试 GET HTTP://本地主机/ API /值$ inlinecount = XXXXX 我得到一个异常,如此看来在Web API应用程序知道 inlinecount

If I try get http://localhost/api/values?$inlinecount=XXXXX I get an exception, so it seems the Web API application knows about inlinecount

<Message>The query specified in the URI is not valid.</Message>
<ExceptionMessage>'xxx' is not a valid value for $inlinecount.</ExceptionMessage> 
<ExceptionType>Microsoft.Data.OData.ODataException</ExceptionType>

我绝对有Microsoft.AspNet.WebApi.OData包 - 这里是程序包管理器控制台

I definitely have the Microsoft.AspNet.WebApi.OData package - here is the output of the Package Manager Console

PM> Install-Package Microsoft.AspNet.WebApi.OData 
Attempting to resolve dependency 'Microsoft.Net.Http (= 2.0.20710.0 && < 2.1)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Client (= 4.0.20710.0 && < 4.1)'.
Attempting to resolve dependency 'Newtonsoft.Json (= 4.5.6)'.
Attempting to resolve dependency 'Microsoft.AspNet.WebApi.Core (= 4.0.20710.0 && < 4.1)'.
Attempting to resolve dependency 'Microsoft.Data.OData (= 5.2.0 && < 5.3.0)'.
Attempting to resolve dependency 'System.Spatial (= 5.2.0)'.
Attempting to resolve dependency 'Microsoft.Data.Edm (= 5.2.0)'.
'Microsoft.AspNet.WebApi.OData 4.0.0' already installed.
WebServicesProject already has a reference to 'Microsoft.AspNet.WebApi.OData 4.0.0'.

有什么建议?

推荐答案

大问题。

$ inlinecount开箱即用,当你发回的OData反应才有效。这样做的原因是,OData的定义中的数据的特殊字段XML和JSON没有定义。因此,在一个OData的响应可能是这样的:

$inlinecount out of the box only works when you're sending back OData responses. The reason for this is that OData defines special fields in the data that XML and JSON don't define. So in OData a response might look like this:

{
  "odata.metadata":"http://localhost:12345/odata/$metadata#Customers",
  "odata.count":"4",
  "value":[ ... ]
}

注意与odata.count属性包装。这是从默认XML和JSON格式器写出数据的方式不同,因为它们不具有该附加信息的封装。所以其他格式化程序在默认情况下保持不变。

Notice the wrapper with the "odata.count" property. This is different from the way the default XML and JSON formatters write out data because they don't have wrappers for this additional information. So other formatters are by default unchanged.

现在,你有几种选择:

您可以选择使用的OData格式。对于这一点,你要按照这个博客帖子的说明:

You could choose to use the OData format. For this, you'll want to follow the instructions in this blog post:

<一个href=\"http://blogs.msdn.com/b/webdev/archive/2013/01/29/getting-started-with-asp-net-webapi-odata-in-3-simple-steps.aspx\">http://blogs.msdn.com/b/webdev/archive/2013/01/29/getting-started-with-asp-net-webapi-odata-in-3-simple-steps.aspx

您也可以选择,而不是返回 PageResult&LT; T&GT; 。这看起来是这样的:

You could also choose to instead return a PageResult<T>. This looks like this:

public PageResult<Customer> Get(ODataQueryOptions<Customer> queryOptions)
{
    IQueryable results = queryOptions.ApplyTo(_customers.AsQueryable());
    return new PageResult<Customer>(results as IEnumerable<Customer>, Request.GetNextPageLink(), Request.GetInlineCount());
}

这应该通过增加对XML和JSON的包装对象可以包括伯爵和下一个页面链接做工精细的OData的,JSON和XML。

This should work fine for OData, JSON, and XML by adding a wrapper object for XML and JSON that can include the Count and the next page link.

这篇关于网页API的OData Inlinecount不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆