星空网 > 软件开发 > ASP.net

ASP.NET Web API实现缓存的2种方式

 

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.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实现缓存"中也做了总结。

 




原标题:ASP.NET Web API实现缓存的2种方式

关键词:ASP.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

人民币升值:https://www.goluckyvip.com/tag/13365.html
利润款选品:https://www.goluckyvip.com/tag/13366.html
选品专家选品:https://www.goluckyvip.com/tag/13367.html
热搜选品:https://www.goluckyvip.com/tag/13368.html
热销选品:https://www.goluckyvip.com/tag/13369.html
telec:https://www.goluckyvip.com/tag/1337.html
武陵山大裂谷周围景点 武陵山大裂谷周围景点图片:https://www.vstour.cn/a/411233.html
南美旅游报价(探索南美洲的旅行费用):https://www.vstour.cn/a/411234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流