【问题标题】:Where to set no-cache header - in pageload or page init (in .net)在哪里设置无缓存标头 - 在页面加载或页面初始化中(在 .net 中)
【发布时间】:2013-09-30 08:45:32
【问题描述】:

我的网页使用无缓存机制,不让浏览器缓存内容。但是,我有一些疑问。开发者没有在 MasterPage 的 PageLoad 方法中插入缓存代码。

是这样的(在主页加载页面

   HttpContext.Current.Response.CacheControl = "no-cache"
   HttpContext.Current.Response.AddHeader("Pragma", "no-cache")
   HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.Date
   HttpContext.Current.Response.Expires = -1

我怀疑这是正确的操作,是否应该更好地将其插入BasePage OnInit 事件中?

【问题讨论】:

  • 没问题,只要在生成Response之前加上就可以了

标签: asp.net header browser-cache


【解决方案1】:

您可以在生成响应之前执行此 HTTPHeaders。 Check MSDN

Page_init/page_loadMasterPage/BasePage/ActualPage

参考Disabling browser caching for all browsers from ASP.NET

你也可以在页面指令中这样做

<%@ OutputCache Duration="60" VaryByParam="None"%>

即使在 Web.config 中你也可以配置

<caching>
  <outputCacheSettings>
    <outputCacheProfiles>
      <add name="Cache30Seconds" duration="30" 
        varyByParam="none" />
    </outputCacheProfiles>
  </outputCacheSettings>
</caching>

More Information on Caching

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 1970-01-01
    • 2012-03-30
    • 2020-10-11
    • 2011-06-30
    • 2013-07-10
    • 2021-06-28
    相关资源
    最近更新 更多