简体   繁体   English

Web API OData V3 `$inlinecount` 失败

[英]Web API OData V3 `$inlinecount` fails

I am using the out of the box ValuesController in a ASP.NET Web API application我在 ASP.NET Web API 应用程序中使用开箱即用的 ValuesController

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

When I get http://localhost/api/values?$inlinecount=allpages当我get http://localhost/api/values?$inlinecount=allpages

This is the response这是回应

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

I have uncommented config.EnableQuerySupport();我没有注释config.EnableQuerySupport();

Filtering, sorting work fine.过滤,排序工作正常。

If I try get http://localhost/api/values?$inlinecount=XXXXX I get an exception, so it seems the Web API application knows about inlinecount如果我尝试get http://localhost/api/values?$inlinecount=XXXXX我得到一个异常,所以看起来 Web API 应用程序知道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>

I definitely have the Microsoft.AspNet.WebApi.OData package - here is the output of the Package Manager Console我肯定有 Microsoft.AspNet.WebApi.OData 包 - 这是包管理器控制台的输出

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

Any suggestions?有什么建议么?

Great question.很好的问题。

$inlinecount out of the box only works when you're sending back OData responses.开箱即用的 $inlinecount 仅在您发回 OData 响应时有效。 The reason for this is that OData defines special fields in the data that XML and JSON don't define.这样做的原因是 OData 定义了 XML 和 JSON 未定义的数据中的特殊字段。 So in OData a response might look like this:因此,在 OData 中,响应可能如下所示:

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

Notice the wrapper with the "odata.count" property.注意带有“odata.count”属性的包装器。 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.这与默认 XML 和 JSON 格式化程序写出数据的方式不同,因为它们没有用于此附加信息的包装器。 So other formatters are by default unchanged.所以其他格式化程序默认情况下保持不变。

Now you have several options:现在你有几个选择:

You could choose to use the OData format.您可以选择使用 OData 格式。 For this, you'll want to follow the instructions in this blog post:为此,您需要按照此博客文章中的说明进行操作:

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

You could also choose to instead return a PageResult<T> .您也可以选择返回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());
}

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.这应该适用于 OData、JSON 和 XML,方法是为 XML 和 JSON 添加一个包装器对象,其中可以包含 Count 和下一页链接。

In OData v4, $inlinecount=allpages has been replaced by $count=true.在 OData v4 中,$inlinecount=allpages 已替换为 $count=true。

$count just works with returning an IQueryable in the recent versions of aspnet mvc. $count 仅适用于在最新版本的 aspnet mvc 中返回 IQueryable。

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

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