Link to home
Create AccountLog in
Avatar of Cjames
CjamesFlag for United States of America

asked on

prevent page caching

We've got a website which is having intermittent caching problems -- as soon as the user clears their cache the problems are not recreatable for some unforceable time for that user.  The website is hosted at GoDaddy, so I can't change IIS settings.  This caching problem is threatening the entire business model.

I should mention this appears to be affecting IE8 only, it has not been known to happen on IE7, Safari, Chrome, or Firefox.

I have put Response.Cache.SetCacheability(HttpCacheability.NoCache); in the page load, I have put <META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="Expires" CONTENT="-1">  in the header.

I just found the following, do I need to put this on every page, will it fix the problem, or do we need to find a host that will turn off caching and does anybody have any idea what host does that?

 Response.ClearHeaders();
        Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1
        Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1
        Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1
        Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1
        Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1  
        Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1  
        Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1  
        Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1  
        Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1  
        Response.AppendHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT"); // HTTP 1.1

Thanks
Avatar of Nitesh Luharuka
Nitesh Luharuka
Flag of India image

you can set these in global.asax/web.config  file
Avatar of Amandeep Singh Bhullar
Add the following line on top of the Page_Load event handler and your ASP.NET page will not be cached in the users browsers:

Response.Cache.SetCacheability(HttpCacheability.NoCache)


You can configure the application's cache API in your Web.config file. As with the page output cache, application hosters can set configuration properties in the Machine.config file and lock cache configuration settings for all applications. The application cache API is configured in the CacheSection. For example, you can disable item expiration with the following configuration element:

<cache disableExpiration="true" />

You can also specify other application cache API configuration settings by assigning values to attributes such as DisableExpiration and DisableMemoryCollection within the configuration file's CacheSection.
Source Article: http://msdn.microsoft.com/en-us/library/ms178606.aspx
http://www.codekeep.net/snippets/82f96fd3-9374-4b1b-8c3a-e83ca2d49682.aspx

private void Page_Load(object sender, System.EventArgs e)

{

if(!Page.IsPostBack)

{

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.Cache.SetAllowResponseInBrowserHistory(false);

}

}
This worked for me

Response.Buffer = true;
Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
Response.Expires = -1500;
Response.CacheControl = "no-cache";
Response.Cache.SetNoStore();

you can try this

<%OutputCache Location="None"%>

Open in new window

Hi There,
caching is handled differently by different browsers.
I had this same issue when I was working on Bank's application.

The headers you are passing will work on all the browsers.
do you use master file or have a header file which is included in all files?
you can call this function from there.


Avatar of Cjames

ASKER

pradyahuja -- By all the headers, you mean the ones beginning with Response.ClearHeaders?

Thanks! Knowing that would be a big help.

No master page or header file -- I couldn't get the desired functionality -- maybe when things calm down I can refactor.  
ASKER CERTIFIED SOLUTION
Avatar of pradyahuja
pradyahuja
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
I am bit disagree with the selection of answer. I have already provided all same options in my comment # 26598443. there is a link provided which have everything you wanted.
hi Vora,
your solution will not work on chrome
this links http://www.codekeep.net/snippets/82f96fd3-9374-4b1b-8c3a-e83ca2d49682.aspx which i had already posted , provide the same result which you had provided

So this means your solution will also not work in chrome.
Avatar of Cjames

ASKER

Sorry vora. You did not confirm my "new code" would solve the problem AND you told me to do something I indicated I was already doing that didn't fix the problem, which indicated to me you hadn't read the question.  
@pradyahuja I was thinking that you had copied a code from that link which i had provided as both are totally Identical. I had provided link on 17 feb and you have answered on 18th Feb.

--Bhaumik
mate, I dint provide any code at all...

look carefully..
I explained the code...
and confirmed that the code in question will work.
as I have done same thing in our staging servers.
cool it guys.... shouldn't be this much hot discussion in summer!!!! (at least here at my place).

@Cjames,

>>rm my "new code" would solve the problem <<

what do you mean by this? as I don't see any new code in any of the post after question.

@pradyahuja,
>>I explained the code...<<

yes, true, there is small explanation.

@vora_bhaumik,

don't be so rude. may not be copied from the link you have provided but I agree that the link you provided and the answer selected both are identical and there is a big difference in time too.

-----------------------

anyway, as per me there should be at least split.

I copied the code from question itself....
Ritesh, what r u talking about...
Summer... seems like we from same country :)
or at least are neighbors
Yes Predy Ahuja. I am from Gujarat - India. not much active in .NET zone, just used to come sometimes and seen good discussion so jump in it!!!!
Avatar of Cjames

ASKER

The new code is in the initial post where I indicated "I found this" starting with response.clearheaders.   Which I will point out is pretty much the same as the code both provided. What I really needed was confirmation that it would solve the problem.

Explanation of points awarded:

Since I had already posted that same code in the initial post my award of points was Not based on the link or the code in the answer selected. Points were awarded based on confirming that code would resolve the problem.
oh.. u from Gujarat...
I thought it's still winters in India...
I am from Oz..
winters are approaching here :)
Avatar of Cjames

ASKER

I certainly didn't mean to hurt anybody's feelings!  It is simply one response provided what I needed and that's the one I accepted.