gpt4 book ai didi

count - OData v4 WebAPI 响应中的项目计数

转载 作者:行者123 更新时间:2023-12-03 07:20:55 26 4
gpt4 key购买 nike

如何返回 OData v4 HTTP 响应中的项目数?

我需要这个数字来分页,所以它应该是过滤后、“跳过”和“顶部”之前的项目数。

我已经尝试在 url 的查询选项中传递 '$inlinecount=allpages' 和 '$count=true' 参数(https://damienbod.wordpress.com/2014/06/13/web-api-and-odata-v4-queries-functions-and-attribute-routing-part-2/ - “$count 的示例”),但我来自 WebAPI 的响应始终只有查询结果(集合) - 整个响应如下所示:

[
{
"Name":"name1",
"age":5
},
{
"Name":"name2",
"age":15
}
]

响应中没有类似“odata.count”的内容。

我还尝试在 WebAPI Controller 操作中返回 PageResult 而不是 IQueryable(如此处所述: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options#server-paging ),但 Request.GetInlineCount() 已弃用,并且其值始终为 null。

有什么想法吗?

[更新]我刚刚在这里发现了同样的问题:WebApi with Odata NextPage and Count not appearing in the JSON response我删除了 [EnableQuery] 属性,现在我的响应如下所示:

{
"Items":
[
{
"Name":"name1",
"age":5
},
{
"Name":"name2",
"age":15
}
],
"NextPageLink":null,
"Count":null
}

但“Count”仍然始终为空。 :(

<小时/>编辑:在 Controller 的请求属性中调试和搜索计数值后,我发现正确的计数值位于名为“System.Web.OData.TotalCount”的属性中。所以现在我从该请求属性中提取该值,我的 Controller 如下所示:

public PageResult<People> Get(ODataQueryOptions<People> queryOptions)
{
var query = _context.People.OrderBy(x => x.SomeProperty);
var queryResults = (IQueryable<People>)queryOptions.ApplyTo(query);
long cnt = 0;
if (queryOptions.Count != null)
cnt = long.Parse(Request.Properties["System.Web.OData.TotalCount"].ToString());

return new PageResult<People>(queryResults, null, cnt);
}

它工作得很好,但我仍然不知道为什么我必须使用这样的解决方法。

最佳答案

供将来引用(OData v4):

首先$inlinecount OData v4 不支持它所以你应该使用$count=true相反。

第二,如果你有正常的ApiController然后您返回类似 IQueryable<T> 的类型您可以通过这种方式附加 count返回结果的属性:

using System.Web.OData;
using System.Web.OData.Query;
using System.Web.OData.Extensions;

//[EnableQuery] // -> If you enable globally queries does not require this decorator!
public IHttpActionResult Get(ODataQueryOptions<People> queryOptions)
{
var query = _peopleService.GetAllAsQueryable(); //Abstracted from the implementation of db access. Just returns IQueryable<People>
var queryResults = (IQueryable<People>)queryOptions.ApplyTo(query);
return Ok(new PageResult<People>(queryResults, Request.ODataProperties().NextLink, Request.ODataProperties().TotalCount));
}

Note: OData functionality does not supported by ApiControllers so you cannot have things like count or $metadata. If you choose to use simple ApiController the way above is the one you should use to return a count property.

<小时/>

为了完全支持 OData 功能,您应该实现 ODataController方法如下:

PeopleController.cs

using System.Web.OData;
using System.Web.OData.Query;

public class PeopleController : ODataController
{
[EnableQuery(PageSize = 10, AllowedQueryOptions = AllowedQueryOptions.All)]
public IHttpActionResult Get()
{
var res = _peopleService.GetAllAsQueryable();
return Ok(res);
}
}

App_Start\WebApiConfig.cs

public static void ConfigureOData(HttpConfiguration config)
{
//OData Models
config.MapODataServiceRoute(routeName: "odata", routePrefix: null, model: GetEdmModel(), batchHandler: new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));
config.EnsureInitialized();
}

private static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder
{
Namespace = "Api",
ContainerName = "DefaultContainer"
};
builder.EntitySet<People>("People").EntityType.HasKey(item => item.Id); //I suppose the returning list have a primary key property(feel free to replace the Id key with your key like email or whatever)
var edmModel = builder.GetEdmModel();
return edmModel;
}

然后您可以通过以下方式访问 OData Api(示例):

编码后的 uri:

http://localhost:<portnumber>/People/?%24count=true&%24skip=1&%24top=3

解码:

http://localhost:<portnumber>/People/?$count=true&$skip=1&$top=3

引用文献:

关于count - OData v4 WebAPI 响应中的项目计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27545207/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com