renathy
renathy

Reputation: 5355

Where to set no-cache header - in pageload or page init (in .net)

I have web page that uses no-cache mechanism for not to let browser to cache content. However, I have some doubts. The developer has inserted no cache code into the PageLoad method in MasterPage.

It is like this (on Page Load in Master Page)

   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

I have some doubt that this is the correct action and whether this could should be better inserted into BasePage OnInit event?

Upvotes: 1

Views: 3896

Answers (1)

Murali Murugesan
Murali Murugesan

Reputation: 22619

You could do this HTTPHeaders before you generate the response. Check MSDN

Page_init/page_load or MasterPage/BasePage/ActualPage

Refer Disabling browser caching for all browsers from ASP.NET

Also you could do this in page directive

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

Even in Web.config you can configure

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

More Information on Caching

Upvotes: 2

Related Questions