带有 Odata Next Page 和 Count 的 Web Api 未出现在 JSON 响应中

2024-05-18

我有一个 webapi 方法,我想打开 oData 分页等。 我按照中的例子http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options

我的方法如下所示:

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] /cdn-cgi/l/email-protection",
    "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] /cdn-cgi/l/email-protection",
    "Organisation": null,
    "IsLockedOut": false,
    "Roles": [
      "Service Administrator"
    ]
  }
]

这应该有效。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

带有 Odata Next Page 和 Count 的 Web Api 未出现在 JSON 响应中 的相关文章

随机推荐