asp.net-mvc – 如果ActionResult未更改,则将MVC.NET OutputCach
介绍问题
如果服务器指示304 Not Modified,我们已成功配置浏览器缓存以返回已保存的响应.这是配置: <caching> <outputCacheSettings> <outputCacheProfiles> <add name="TransparentClient" location="Client" duration="0" /> </outputCacheProfiles> </outputCacheSettings> </caching> web.config是完美的,并设置Cache-control:private,max-age = 0,以便: >浏览器将缓存响应, 问题是我们的MVC.NET动作总是响应200而不是304. 问题 当ActionResult没有改变时,我们如何配置输出缓存以返回304 Not Modified? > MVC.NET中是否有内置的缓存验证? roll-our-own可能需要一个带ETag或Last-Modified的Action Filter. 屏幕截图 这是一个Fiddler截图,显示缺少304. > 318是SHIFT刷新. 搜索和研究 ASP.NET MVC : how do I return 304 “Not Modified” status?提到从Action中返回304.这并没有提供使OutputCache准确响应304的方法. Working with the Output Cache and other Action Filters显示了如何覆盖OnResultExecuted,这将允许添加/删除标头. 解决方法
以下内容适用于我们.
Web.Config中 设置Cache-Control:private,max-age-0以启用缓存并强制重新验证. <system.web> <caching> <outputCacheSettings> <outputCacheProfiles> <add name="TransparentClient" duration="0" location="Client" /> </outputCacheProfiles> </outputCacheSettings> </caching> </system.web> 行动 如果未修改响应,则回复304. [MyOutputCache(CacheProfile="TransparentClient")] public ActionResult ValidateMe() { // check whether the response is modified // replace this with some ETag or Last-Modified comparison bool isModified = DateTime.Now.Second < 30; if (isModified) { return View(); } else { return new HttpStatusCodeResult(304,"Not Modified"); } } 过滤 删除Cache-Control:private,max-age-0否则缓存将存储状态消息. public class MyOutputCache : OutputCacheAttribute { public override void OnResultExecuted(ResultExecutedContext filterContext) { base.OnResultExecuted(filterContext); if (filterContext.HttpContext.Response.StatusCode == 304) { // do not cache the 304 response filterContext.HttpContext.Response.CacheControl = ""; } } } Fidder酒店 Fiddler表明缓存行为正常. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc – ASP.NET MVC路由中未传递的参数值
- ASP.Net 3.5 / 4.0 CodeBehind还是CodeFile?
- asp.net-web-api – 带有json有效负载中令牌的WebApi授权过
- asp.net-mvc – 您对Telerik Extensions for MVC有何看法?
- asp.net-mvc – ALTER TABLE DROP COLUMN失败,因为表’yyy’
- 如何下载特定版本的ASP.NET MVC 5源代码
- asp.net – 在新的MVC4应用程序中获取UserId(int)
- asp.net – aspx.designer.cs它是如何工作的?
- 详解 ASP.NET Core MVC 的设计模式
- asp.net-mvc-3 – MVC3:设置下拉列表选择的值