源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

一个伴随ASP.NET从1.0到4.0的OutputCache Bug介绍

  • 时间:2022-11-04 17:04 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:一个伴随ASP.NET从1.0到4.0的OutputCache Bug介绍
我们先来一睹这个Bug的风采! 在一个.aspx文件中增加OutputCache设置,代码如下:
[url=http://www.asp.net/LEARN/whitepapers/aspnet4/breaking-changes#0.1__Toc256770157]ASP.NET 4 Breaking Changes[/url]中专门提到了这个bug —— "Output Caching Changes to Vary * HTTP Header": [quote] In ASP.NET 1.0, a bug caused cached pages that specified Location="ServerAndClient" as an output–cache setting to emit a Vary:* HTTP header in the response. This had the effect of telling client browsers to never cache the page locally. In ASP.NET 1.1, the System.Web.HttpCachePolicy.SetOmitVaryStar method was added, which you could call to suppress the Vary:* header. This method was chosen because changing the emitted HTTP header was considered a potentially breaking change at the time. However, developers have been confused by the behavior in ASP.NET, and bug reports suggest that developers are unaware of the existing SetOmitVaryStar behavior. In ASP.NET 4, the decision was made to fix the root problem. The Vary:* HTTP header is no longer emitted from responses that specify the following directive: <%@OutputCache Location="ServerAndClient" %> As a result, SetOmitVaryStar is no longer needed in order to suppress the Vary:* header. In applications that specify Location="ServerAndClient" in the @ OutputCache directive on a page, you will now see the behavior implied by the name of the Location attribute's value – that is, pages will be cacheable in the browser without requiring that you call the SetOmitVaryStar method. [/quote] 从上面的文档中我们可以知道这个Bug的历史: 在ASP.NET 1.0时,如果在OutputCache中设置Location="ServerAndClient",在ASP.NET在响应时会浏览器发送Vary:* HTTP header。 在ASP.NET 1.1时,微软针对这个Bug,提供一个专门的方法System.Web.HttpCachePolicy.SetOmitVaryStar(bool omit),通过SetOmitVaryStar(true)修改HTTP header,去掉Vary:*。 在ASP.NET 4时,微软郑重地宣布从根本上解决了这个问题。 而且,文档中提到这个bug只会出现在Location="ServerAndClient"时。 可是,我用ASP.NET 4的实测试情况是:不仅Location="ServerAndClient"时的Bug没有解决,而且Location="Any"时也会出现同样的Bug。 [b]解决方法[/b] 解决方法很简单,只要用ASP.NET 1.1时代提供的System.Web.HttpCachePolicy.SetOmitVaryStar(bool omit)就能解决问题,只需在Page_Load中添加如下代码:
[url=http://www.andypotts.com/Blog/CommentView,guid,05dcac0c-1d46-42ba-8d3c-b2eb5b36b45a.aspx]ASP.NET caching tests find a bug with VaryByParam[/url] [url=http://stackoverflow.com/questions/2624181/how-to-cache-asp-net-web-site-for-better-performance]How to cache asp.net web site for better performance[/url] [url=http://connect.microsoft.com/VisualStudio/feedback/details/254940/the-serverandclient-parameter-with-the-outputcache-page-directive-does-not-cache-on-the-client-without-code]Microsoft Connect: The ServerAndClient parameter with the OutputCache page directive does not cache on the client, without code[/url] [b]小结 [/b]小bug,解决方法也很简单。但是,如果你不知道这个bug,又会陷入微软的一个骗局(之前提到一个WCF Client的[url=http://www.cnblogs.com/dudu/archive/2011/11/02/wcf_client_no_using_call.html]骗局[/url]),不知不觉中浪费了服务器资源与带宽。 微软那么有钱,有那么多天才程序员,可是Bug也很难避免,可见开发优秀的软件是多么具有挑战性的工作! [b]补充[/b] ASP.NET MVC 中不存在这个问题。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部