今天我们来看看Web API的数据查询功能,尽管之前介绍CRUD的文章里面提到过怎么去Read数据,可是并没有详细的去深究那些细节,今天我们就来详细看看吧。事实上呢,Web API的数据查询接口也是基于OData协议的,所以之前的OData Url Query的构造规则没有非常大的变化。比如:$top, $select, $filter, $expand, $order的功能还是在的,只是也加入了一些新东西,比如

$count  -- 返回记录的总数

Paging Mechanism(分页机制)-- 来东西,如今,实现机制不一样了,基于HTTP报头设置页大小

Formatted Value(新东西。没有找到最新的文档) -- 目測和记录的格式化有关,只是没找到详细的Mapping文档

$count

$count非常好理解,返回记录在系统中的总数(和filter条件相关),假设不制定filter条件。则返回全部记录的总数。

Paging Mechanism

须要设置HTTP 报头:odata.maxpagesize=?,和之前的API不一样了

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" style="font-family: FangSong_GB2312;font-size:14px;" alt="" />

 HttpRequestMessage retrieveAccReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/accounts?$select=name&$count=true");

            retrieveAccReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode);
retrieveAccReq.Headers.Add("Prefer", "odata.maxpagesize=2");
string nextPageLink = string.Empty;
JObject result = null;
int pageIndex = 1;
HttpResponseMessage retrieveAccResp;
do
{
if (!string.IsNullOrWhiteSpace(nextPageLink))
{ retrieveAccReq = new HttpRequestMessage(HttpMethod.Get, nextPageLink);
retrieveAccReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode);
retrieveAccReq.Headers.Add("Prefer", "odata.maxpagesize=2");
} retrieveAccResp = await client.SendAsync(retrieveAccReq);
result = JsonConvert.DeserializeObject<JObject>(await retrieveAccResp.Content.ReadAsStringAsync());
Console.WriteLine("Page-" + pageIndex++);
Console.WriteLine(result.ToString());
if (retrieveAccResp.IsSuccessStatusCode)
{ if (result["@odata.nextLink"] != null && !string.IsNullOrWhiteSpace(result["@odata.nextLink"].Value<string>()))
{
nextPageLink = result["@odata.nextLink"].Value<string>();
}
else
{
nextPageLink = "";
} }
else
{
break;
} } while (!string.IsNullOrEmpty(nextPageLink));

Formatted Value

目測和返回记录的格式化有关,返回的记录更好理解,只是没找到详细的Formatted Mapping。建议不轻易使用。

            HttpRequestMessage retrieveAccWithFormattedValueReq = new HttpRequestMessage(HttpMethod.Get, webApiUrl + "/accounts?$select=name,donotpostalmail,accountratingcode,numberofemployees,revenue&$top=1");
retrieveAccWithFormattedValueReq.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessCode);
retrieveAccWithFormattedValueReq.Headers.Add("Prefer", " odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\""); HttpResponseMessage retrieveAccWithFormatedValueResp = await client.SendAsync(retrieveAccWithFormattedValueReq); if (retrieveAccWithFormatedValueResp.IsSuccessStatusCode)
{
JObject result = JsonConvert.DeserializeObject<JObject>(await retrieveAccWithFormatedValueResp.Content.ReadAsStringAsync());
Console.WriteLine(result.ToString());
}

在Web API的数据查询里面呢,另一个比較重要的仅仅是就是使用Web API Query Function,博主将在兴许的博文中为大家介绍它的用法。

最新文章

  1. python 列表与元组的操作简介
  2. 解压版MySQL安装说明
  3. nodeJS实战
  4. &quot;jobTracker is not yet running&quot;(hadoop 配置)
  5. How to solve GM MDI cannot complete the installation
  6. WF工作流与管理类应用系统工作流需求实现的一些误区
  7. 有意思的字符串反转(JavaScript)
  8. Mono For Android中简单实现按钮的动画效果
  9. UVa 10057 - A mid-summer night&#39;s dream
  10. (原)centos7安装和使用greenplum4.3.12(详细版)
  11. 关于wordpress升级遇到的问题
  12. 第二章 python变量及文件
  13. Hive基础之Hive数据类型
  14. python mysql 视图 触发器 事物 存储过程 用户授权 数据备份还原
  15. Android jks 签名文件 生成
  16. java高并发编程(二)
  17. CentOS6.5安装Redis数据库
  18. 在linux安装redis单机和集群后,如何在windows上使用redis客户端或者java代码访问错误的原因很简单,就是没有连接上redis服务,由于redis采用的安全策略,默认会只准许本地访问。需要通过简单配置,完成允许外网访问。
  19. Nginx常用配置整理
  20. vb6转16进制

热门文章

  1. vim之替换命令
  2. cygin常用命令
  3. [转]python开发_shelve_完整版
  4. 【编程工具】如何用Sublime Text3建立本地服务器和站点
  5. Leetcode 327.区间和的个数
  6. [android开发篇]权限列表
  7. 封装自己DB
  8. redis介绍和安装(一)
  9. ios弹性头部
  10. BZOJ 1297: [SCOI2009]迷路 [矩阵快速幂]