ASP.NET Web API实现缓存的2种方式
来源:互联网 发布:javascript注释 编辑:程序博客网 时间:2024/06/10 16:30
在ASP.NET Web API中实现缓存大致有2种思路。一种是通过ETag, 一种是通过类似ASP.NET MVC中的OutputCache。
通过ETag实现缓存
首先安装cachecow.server
install-package cachecow.server
在WebApiConfig中。
public static class WebApiConfig
{ public static HttpConfiguraiton Register() { var config =new HttpConfiguration(); //支持通过特性设置路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( "DefaultRouting", "api/{controller}/{id}", defaults:new {id = RouteParamter.Optional} ); //config.Formatters.JsonFormatter.SupportedMediaTypes .Add(new MediaTYpeHeaderValue("text/html")); config.Formatters.XmlFormatter.SupportedMediaType.Clear(); config.Foramtters.JsonFormatter.SuppoortedMediaTypes.Add( new MediaTypeHeaderValue("application/json-patch+json"); ); config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented; config.Formatters.JsonFormatter.SerializerSettings.ContractResolver =new CamelCaseProeprtyNamesContractResolver(); //HTTP缓存 默认缓存在内存中 config.MessageHandlers.Add(new CacheCow.Server.CachingHandler(config)); return config; }}
→ 客户端发出请求
GET http://localhost:43321/api/groups/1
→ 返回200状态码,在响应的Headers中:
ETag:W/"..."
Last-Modified:...
→ 再次请求,通过If-None-Match属性把ETag带上。
GET http://localhost:43321/api/groups/1
Host:localhost:43321
If-None-Match:ETag:W/""
→ 返回304状态码
通过OutputCache实现缓存
在ASP.NET Web API中实现缓存的另外一种思路是通过类似ASP.NET MVC中的OutputCache,具体可参考:Strathweb.CacheOutput.WebApi2
有关ASP.NET Web API缓存,在"ASP.NET Web API中通过ETag实现缓存"中也做了总结。
0 0
- ASP.NET Web API实现缓存的2种方式
- 关于ASP.NET中的缓存问题的几种方式
- ASP.net中常用的缓存方式
- Asp.net WEB视频实现方式
- asp.net web api
- ASP.NET Web API
- asp.net web api
- asp.net web api
- asp.net web api
- 8 种提升 ASP.NET Web API 性能的方法
- 6种提升ASP.NET Web API性能的方法
- 8 种提升 ASP.NET Web API 性能的方法
- 8种提升ASP.NET Web API性能的方法
- ASP.NET Web API 2 对 CORS 的支持
- asp.net web API 的调用返回Json值 POST方式请求
- ASP.NET MVC与ASP.NET Web API的区别
- ASP.NET MVC与ASP.NET Web API的区别
- 入门 ASP.NET Web API 2 (C#)
- 【VS开发】visual studio 2015的NuGet Manager解决方案管理功能
- struct hostent结构体使用
- select函数使用时应注意的问题
- 【Thinking in Java】操作符
- JS
- ASP.NET Web API实现缓存的2种方式
- LINQ to SQL语句之用户定义函数
- DataReader和DataSet的异同
- LINQ to SQL语句之DataContext
- HDU 5726 求gcd=k的区间的个数 (二分+RMQ)
- stringbuilder
- hibernate参数设置一览表
- 矩阵转置与矩阵相乘
- 二维数组和指针数组