2014-04-04 17 views
1

我有一个webapi方法,我想切换oData分页等。 我跟着http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-optionsWebApi Odata NextPage和计数没有出现在JSON响应

我的方法看起来像例如:

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

该计数器填充,下一个页面链接是存在的,并且应用了正确的OData选项即排序顺序等。当我回来它在我的API方法中,正确的数据返回,但计数和下一个链接不出现在我的JSON中。

我错过了一个设置来打开它吗?

即这是我的JSON响应:

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": "[email protected]", 
    "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": "[email protected]", 
    "Organisation": null, 
    "IsLockedOut": false, 
    "Roles": [ 
     "Service Administrator" 
    ] 
    } 
] 

回答

2

这应该工作。

如果将[EnableQuery]属性应用于您的方法或控制器,请务必删除[EnableQuery]属性。这将导致返回的JSON不包括计数和下一个链接。