WebApi with Odata NextPage and Count not appearing in the JSON response

…衆ロ難τιáo~ 提交于 2020-01-15 03:17:32

问题


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"
    ]
  }
]

回答1:


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.



来源:https://stackoverflow.com/questions/22854312/webapi-with-odata-nextpage-and-count-not-appearing-in-the-json-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!