no cache

Given below is the code I am using in a .net application to prevent browser from caching -- I'm not sure why but this is not working. I also used the script -- Response.Cache.SetNoStore() in the page load event of the masterpage and that too is not working.

any help would be much appreciated
<%  System.Web.HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache")
System.Web.HttpContext.Current.Response.Expires = 0
    System.Web.HttpContext.Current.Response.Cache.SetNoStore()
    System.Web.HttpContext.Current.Response.AddHeader("Pragma", "no-cache")%>

Open in new window

sanagarwlAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dirk HaestProject managerCommented:
Disable Page Caching
Source: http://coercedcode.blogspot.com/2007/08/another-off-topic-post-but-that-is-ok.html

There are many pages on the out there with information about how to disable page caching. I could not find any which were correct. A lot of the information was partially correct, but it didn't work for the newer Firefox browsers.

I tested all this by making a test page where I display a timestamp and move to another page, then I hit the back button to see if the timestamp is updated or if it is just showing me the old one.

Previously with just the first three lines of this function which was my old disable page cache function it worked fine with Internet Explorer, but it was not working with Firefox. I do not remember if it used to work for Firefox, but I know it wasn't working for me now.

Now I think I have figured out and it seems to be working for both IE and Firefox. So without further delay here is the code for all you aspiring programmers out there.

        public static void DisablePageCaching(Page page)

        {

            page.Response.Cache.SetExpires(DateTime.Now.AddDays(-30));

            page.Response.Cache.SetCacheability(HttpCacheability.NoCache);

            page.Response.Cache.SetNoServerCaching();

            page.Response.Cache.SetNoStore();

        }

The secret ingredient, the key thing which was missing from my previous implementation is this line:

page.Response.Cache.SetNoStore();

That seems to make Firefox happy.
0
Dirk HaestProject managerCommented:
Disabling Browser Caching In C# and ASP.NET
http://www.352media.com/rantingandraving/rave.aspx?raveid=325&id=212

The majority of advice on how to disable browser caching seems to revolve around how to make it work in IE.  However, in Firefox the fixes don't always seem to work.  Below are some notes and sample code on how to make it work for both browsers:

The SetNoStore() seems to be a necessity for FireFox
The HttpCacheability.NoCache has an issue in IE (not sure about FireFox) when you are on a secure (https) page and are trying to send a file by setting the headers. It causes the file download to not work properly.
SetValidUntilExpires(), SetRevalidation()  unsure if this is a requirement at this time. Was added because it was still giving problems in FireFox before SetNoStore() was found.
SetExpires() I tried to set this to 5 days in the past and I still got the 2 second window.


public static void DisablePageCaching()
{



//Used for disabling page caching
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();


}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.