对于一些返回数据非常简单的 Web API,比如我们今天遇到的“返回指定用户的未读站内短消息数”,返回数据就是一个数字,如果通过 http response body 返回数据,显得有些奢侈。何不直接通过 http headers 返回呢?节能又环保。于是今天在 ASP.NET Web API 中实际试了一下,证明是可行的。

在 Web API 服务端借助 HttpResponseMessage ,可以很轻松地实现,代码如下:

public class MessagesController : ApiController
{
[Route("api/messages/user-{userId}/unread/count")]
public async Task<HttpResponseMessage> GetUserUnreadMessageCount(int userId)
{
var unreadCount = ;
var response = Request.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("X-RESULT-COUNT", unreadCount.ToString());
return response;
}
}

而调用客户端只需直接从 http headers 中读取数据,无需从 http response body 中读取(如果用 HttpClient 就省掉了 Content.ReadAsStringAsync 操作),从而节省了资源。代码如下:

public class WebApiTest
{
[Fact]
public async Task Get_User_Unread_Message_Count()
{
using (var client = new HttpClient())
{
client.BaseAddress = new System.Uri("www.cnblogs.com");
var userId = ;
var response = await client.GetAsync($"/api/messages/user-{userId}/unread/count");
if (response.IsSuccessStatusCode)
{
var unreadCount = response.Headers.GetValues("X-RESULT-COUNT").FirstOrDefault();
Console.WriteLine(unreadCount);
Assert.Equal(, int.Parse(unreadCount));
}
else
{
Console.WriteLine(response.StatusCode);
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
}
}
}

【参考资料】

Getting a count of returns seen by a RESTful request

Paging in ASP.NET Web API: Using HTTP Headers

能省则省:在ASP.NET Web API中通过HTTP Headers返回数据的更多相关文章

  1. ASP.NET Web API中的Controller

    虽然通过Visual Studio向导在ASP.NET Web API项目中创建的 Controller类型默认派生与抽象类型ApiController,但是ASP.NET Web API框架本身只要 ...

  2. 在ASP.NET Web API中使用OData

    http://www.alixixi.com/program/a/2015063094986.shtml 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在A ...

  3. ASP.NET Web API 中的异常处理(转载)

    转载地址:ASP.NET Web API 中的异常处理

  4. 【ASP.NET Web API教程】6.2 ASP.NET Web API中的JSON和XML序列化

    谨以此文感谢关注此系列文章的园友!前段时间本以为此系列文章已没多少人关注,而不打算继续下去了.因为文章贴出来之后,看的人似乎不多,也很少有人对这些文章发表评论,而且几乎无人给予“推荐”.但前几天有人询 ...

  5. Asp.Net Web API 2第十三课——ASP.NET Web API中的JSON和XML序列化

    前言 阅读本文之前,您也可以到Asp.Net Web API 2 系列导航进行查看 http://www.cnblogs.com/aehyok/p/3446289.html 本文描述ASP.NET W ...

  6. ASP.NET WEB API 中的路由调试与执行过程跟踪

    路由调试 RouteDebugger 是调试 ASP.NET MVC 路由的一个好的工具,在ASP.NET WEB API中相应的有 WebApiRouteDebugger ,Nuget安装 Inst ...

  7. ASP.NET Web API中的参数绑定总结

    ASP.NET Web API中的action参数类型可以分为简单类型和复杂类型. HttpResponseMessage Put(int id, Product item) id是int类型,是简单 ...

  8. 【ASP.NET Web API教程】5.5 ASP.NET Web API中的HTTP Cookie

    原文:[ASP.NET Web API教程]5.5 ASP.NET Web API中的HTTP Cookie 5.5 HTTP Cookies in ASP.NET Web API 5.5 ASP.N ...

  9. 【ASP.NET Web API教程】4.3 ASP.NET Web API中的异常处理

    原文:[ASP.NET Web API教程]4.3 ASP.NET Web API中的异常处理 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本系列教程,请先看前面的内 ...

随机推荐

  1. Python自动化 【第一篇】:Python简介和入门

    Python简介: 一.什么是python Python是一门动态解释性的强类型定义语言. pythonde 特点:“优雅”.“明确”.“简单”. 二.Python由来 python的创始人为吉多·范 ...

  2. VS2010 "error MSB8011” 解决方法

    http://blog.csdn.net/heihei36/article/details/8923971 —————————————————————————————————————————————— ...

  3. error C2259: 'CException' : cannot instantiate abstract class

    vc6.0编译通过,VS2008则报错 解决方法: 把CException改为CUserException

  4. linux配置的问题

    1 从系统设置-文本设置中把双拼删掉 2 通过sudo passwd root 修改root密码 3 通过su获取root权限 4 通过sudo pppoeconf输入宽带帐号密码 5 把更新源修改成 ...

  5. URLConnection 和 HttpClients 发送请求范例

    . java.net.URLConnection package test; import java.io.BufferedReader; import java.io.IOException; im ...

  6. Google上的Cookie Matching

    Cookie Matching This guide explains how the Cookie Matching Service enables you to make more effecti ...

  7. C# DataSet和DataTable详解

    1.C# DataSet和DataTable详解:http://www.cnblogs.com/top5/archive/2009/04/23/1441765.html 2.DataSet和DataT ...

  8. SQL数据库完全复制

    很少摸 Windows 环境下的东西,最近被个 MS SQL Server 的数据库搞得头大.实在不像 MySQL 那样用起来轻车熟路, OrZ ... 本来以为企业管理器里面既然提供了 DTS 数据 ...

  9. 时间转换为yyyymmdd

    Convert.ToDateTime(tbinpici.Text).ToString("yyyyMMdd")

  10. java内存分析

    链接:http://blog.csdn.net/chana1101/article/details/5632393