用的WebAPI的OData下一页和数量而不是出现在JSON响应 [英] WebApi with Odata NextPage and Count not appearing in the JSON response
问题描述
我有我想要切换的OData分页等上的WebAPI方法。
我跟着<一个例子href=\"http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options\" rel=\"nofollow\">http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options
我的方法是这样的:
公共PageResult&LT; UserViewModel&GT; GetUsers(ODataQueryOptions&LT; UserViewModel&gt;选项)
{
VAR设置=新ODataQuerySettings()
{
每页= 2
}; VAR的结果= UserLogic.GetUsers(用户ID,的UserManager,_db);
VAR过滤= options.ApplyTo(结果,设置); VAR pagedResult =新PageResult&LT; UserViewModel&GT;(
过滤为IEnumerable&LT; UserViewModel&gt;中
Request.GetNextPageLink(),
Request.GetInlineCount());
返回pagedResult;
}
这计数人口和下一个页面链接是存在的,正确的选项OData的应用,即排序顺序等等。当我在API方法返回它,正确的数据回来,但伯爵和下一个链接不要 ŧ出现在我的JSON。
我缺少一个设置来开启这项功能?
即。这是我的JSON响应:
HTTP / 1.1 200 OK
缓存控制:无缓存
编译:无缓存
内容类型:应用程序/ JSON的;字符集= UTF-8
过期:-1
服务器:Microsoft-IIS / 8.0
的X请求ID:b215962b-6a4a-431D-9850-7ecbf808538e
的X ASPNET-版本:4.0.30319
的X来源档案: =?UTF-8?B?QzpcUmVwb3NpdG9yaWVzXEdpdEh1YlxxbGRyYS1wb3J0YWxccWxkcmEtcG9ydGFsLldlYlxxbGRyYS5iYXNlbGluZS5hcGlcYXBpXHVzZXJz?=
的X已启动方式:ASP.NET
日期:星期五,2014年4月4日五时16分53秒GMT
内容长度:554[
{
ID:500e6f96-b2bd-48d9-8181-5bbc39c673f6
用户名:adam@test.com
组织:{
ID:f179bc35-89b8-e311-9dfd-0050569b4cee
名称:黑与白出租车私人有限公司,
IsActive:真实,
LastUpdatedDate:2014-04-03T11:35:26.167
},
IsLockedOut:假的,
角色:[]
},
{
ID:0d661d1b-9e52-4f2f-BAEC-3eb89197bb6d
用户名:bob@test.com
组织:空,
IsLockedOut:假的,
角色:
服务管理员
]
}
]
这应该工作。
如果被应用到你的方法或控制器它一定要删除[EnableQuery]属性。这将导致返回的JSON为不包括计数和下一个链接。
I have a webapi method that I want to switch oData paging etc on. I followed the example in http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options
My method looks like:
public PageResult<UserViewModel> GetUsers(ODataQueryOptions<UserViewModel> options)
{
var settings = new ODataQuerySettings()
{
PageSize = 2
};
var results = UserLogic.GetUsers(userId, UserManager, _db);
var filtered = options.ApplyTo(results, settings);
var pagedResult = new PageResult<UserViewModel>(
filtered as IEnumerable<UserViewModel>,
Request.GetNextPageLink(),
Request.GetInlineCount());
return pagedResult;
}
That the count is populated and the next page link is there and the correct oData options are applied i.e. sort order etc. When I return it in my api method, the correct data comes back, but the count and next link don't appear in my json.
Am I missing a setting to turn this on?
i.e. this is my json response:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-RequestID: b215962b-6a4a-431d-9850-7ecbf808538e
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcUmVwb3NpdG9yaWVzXEdpdEh1YlxxbGRyYS1wb3J0YWxccWxkcmEtcG9ydGFsLldlYlxxbGRyYS5iYXNlbGluZS5hcGlcYXBpXHVzZXJz?=
X-Powered-By: ASP.NET
Date: Fri, 04 Apr 2014 05:16:53 GMT
Content-Length: 554
[
{
"Id": "500e6f96-b2bd-48d9-8181-5bbc39c673f6",
"UserName": "adam@test.com",
"Organisation": {
"Id": "f179bc35-89b8-e311-9dfd-0050569b4cee",
"Name": "Black and White Cabs Pty Ltd",
"IsActive": true,
"LastUpdatedDate": "2014-04-03T11:35:26.167"
},
"IsLockedOut": false,
"Roles": []
},
{
"Id": "0d661d1b-9e52-4f2f-baec-3eb89197bb6d",
"UserName": "bob@test.com",
"Organisation": null,
"IsLockedOut": false,
"Roles": [
"Service Administrator"
]
}
]
This should work.
Be sure to remove the [EnableQuery] attribute if it is being applied to your method or controller. This will cause the returned JSON to not include the count and next link.
这篇关于用的WebAPI的OData下一页和数量而不是出现在JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!