在你的ASP.NET Web API中使用GZIP 或 Deflate 。 对于减少响应包的大小和响应速度,压缩是一种简单而有效的方式。 这是一个非常有必要使用的功能,你可以查看更多关于压缩的文章在我的博客 ASP.NET Web API GZip compression ActionFilter with 8 lines of code.
5) 使用caching
在Web API方法中使用output caching意义深远.举例来说,如果大量用户访问同一个一天只改变一次的响应(response)内容。 如果你想实现手动缓存,例如把用户口令缓存到内存,请参看我的博文 Simple way to implement caching in ASP.NET Web API.
使用异步的 Web API 服务大幅增加 Web API 对于Http 请求的处理数量。 实现是简单的,只需使用 async 的关键字和 将你方法的返回值类型改为 Task 即可。 [code ][HttpGet]
public async Task OperationAsync()
{
await Task.Delay(2000);
}[/code]
8) 返回多个结果集和集合的组合
减少传输的次数不仅多数据库有好处,对于 Web API同样 ,你才有可能使用结果集的功能。 也就是说你可以从DataReader去提取多个结果集 参见以下演示代码: [code ]// read the first resultset
var reader = command.ExecuteReader();
// read the data from that resultset
while (reader.Read())
{
suppliers.Add(PopulateSupplierFromIDataReader( reader ));
}
// read the next resultset
reader.NextResult();
// read the data from that second resultset
while (reader.Read())
{
products.Add(PopulateProductFromIDataReader( reader ));
}[/code] 你可以在一个 Web API 的一次响应中返回多个对象,试着将你的返回的多个对象进行组合后返回 如下: [code ]public class AggregateResult
{
public long MaxId { get; set; }
public List<Folder> Folders{ get; set; }
public List<User> Users{ get; set; }
}[/code] 这种方式将减少对你的WEB API的HTTP请求。 感谢你读读这篇文章。