Disabling Browser Cache on ASP.NET Pages That Use Master Pages

G

Guest

I am writing an ASP.NET 2.0 application that uses master pages. I have some
pages that must not be cached on the client. In ASP.NET 1.1 I achieved this
using metatags:

<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">

This tags are part of the <head> element. Sample code that I have seen
shows how to add metatags of the form:

<meta name="KEYWORDS" content="html, webdesign, javascript">

Documentation and literature says to use code in the Page_Load event handler
as follows:

base.Master.Page.Header.Metadata.Add("Keywords", "html,...");

However when I attempt this I get a compile error indicating that Header
does not have a Metadata property.

How can I programmatically add the needed metatags to an Master page derived
aspx page to disable client-side caching?

As always, any guidance is greatly appreciated.

Eagle
 
A

Alan Silver

"(e-mail address removed)"
I am writing an ASP.NET 2.0 application that uses master pages. I have some
pages that must not be cached on the client. In ASP.NET 1.1 I achieved this
using metatags:

<meta http-equiv="Expires" content="0">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">

You can do the same in 2.0, or indeed in any HTML-based environment as
this is an HTML issue, nothing to do with the server-side environment.
This tags are part of the <head> element. Sample code that I have seen
shows how to add metatags of the form:

<meta name="KEYWORDS" content="html, webdesign, javascript">

Documentation and literature says to use code in the Page_Load event handler
as follows:

base.Master.Page.Header.Metadata.Add("Keywords", "html,...");

However when I attempt this I get a compile error indicating that Header
does not have a Metadata property.

I seem to remember that this was pulled from ASP.NET after a beta
release. I struggled with it for a while and then found a much easier
way...
How can I programmatically add the needed metatags to an Master page derived
aspx page to disable client-side caching?

Add a contentplaceholder inside the <head> in your master page. Then in
any pages that need these headers, populate the content control with
these meta tags. Other pages don't need to populate (or even have) the
content control.

I do this a lot and it's much easier (and less code) than the
mysteriously vanishing base.Master.Page.Header.MetaData.Add() method,
which struck me as being a very heavy-handed way to do it in the first
place.

HTH
 
J

Juan T. Llibre

I prefer to use :

Response.Cache.SetNoStore();

....instead of using meta tags.
 
A

Alan Silver

Juan T. Llibre said:
I prefer to use :

Response.Cache.SetNoStore();

...instead of using meta tags.

What does that actually do? Does it send extra headers, or does it just
write meta tags for you? Not come across it before.

Either way, my point was that writing meta tags into a content control
is generally much easier than using the Metadata.Add method. Your method
is even easier for this particular issue.

Ta ra
 
J

Juan T. Llibre

re:
What does that actually do?

It prevents the browser from caching the page
by setting the "Cache-Control" http header to "no-cache".

http://msdn2.microsoft.com/en-us/library/system.web.httpcachepolicy.setnostore.aspx

For more information, see RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1 :

http://www.ietf.org/rfc/rfc2616.txt

In particular, for complete details, see Section 13.1.3 "Cache-control Mechanisms" (page 76)
and Section 14.9 "Cache-Control" (page 107)






Alan Silver said:
Juan T. Llibre <[email protected]> said:
I prefer to use :

Response.Cache.SetNoStore();

...instead of using meta tags.

What does that actually do? Does it send extra headers, or does it just write meta tags for you?
Not come across it before.

Either way, my point was that writing meta tags into a content control is generally much easier
than using the Metadata.Add method. Your method is even easier for this particular issue.

Ta ra
 
A

Alan Silver

Juan,

Thanks for the info on that one.

Juan T. Llibre said:
re:
What does that actually do?

It prevents the browser from caching the page
by setting the "Cache-Control" http header to "no-cache".

http://msdn2.microsoft.com/en-us/library/system.web.httpcachepolicy.setn
ostore.aspx

For more information, see RFC 2616: Hypertext Transfer Protocol -- HTTP/1.1 :

http://www.ietf.org/rfc/rfc2616.txt

In particular, for complete details, see Section 13.1.3 "Cache-control
Mechanisms" (page 76)
and Section 14.9 "Cache-Control" (page 107)
 
G

Guest

Thanks for the responses. Sorry, I have not checked this for the last few
days.

I particularly like the Response.Cache.SetNoStore. I have seen somewhere
that setting "Cache-Control" to "no-cache" in the http header does not work
in all browsers.

I did work out a solution based upon a reference from our friend, Dino
Esposito, in his book from MSPress, Programming Microsoft ASP.NET 2.0, on
page 134. This addresses a way to add http-equiv metadata. Specifically,
(straight from the book):

void Page_Init(object sender, EventArgs, e)
{
HtmlMeta meta = new HtmlMeta();
meta.HttpEquiv = "refresh";
meta.Content = Int32.Parse(Content.Text).ToString();
((Control)Header.Controls.Add(meta);
}

This adds a tag <meta http-equiv="refresh" ... /> tag to the page <head>. I
modified this along the line:

HtmlHead hdr = Master.Page.Header;
util.DisableBrowserCaching(hdr); // Here util is a utility/helper class

The DisableBrowserCaching method looks something like this:

void DisableBrowserCaching(HtmlHead hdr)
{
Control ctrl = (Control)hdr;

string currGMT =
DateTime.Now.ToUniversalTime().ToLongDateString() +
" " + DateTime.Now.ToUniversalTime().ToLongTimeString() + "
GMT";

HtmlMeta meta = new HtmlMeta();
meta.Name = "expires";
meta.Content = currGMT;
ctrl.Controls.Add(meta);

meta = new HtmlMeta();
meta.HttpEquiv = "pragma";
meta.Content = "no-cache";
ctrl.Controls.Add(meta);

meta = new HtmlMeta();
meta.HttpEquiv = "cache-control";
meta.Content = "no-cache";
ctrl.Controls.Add(meta);

}

This works and, I think, is more general for the different browser
behaviors. Also, it allows me to set the behavior for any page, whether a
master page is involved or not.

Any comments on this are welcome.

Again thanks for the comments.

Eagle


Alan Silver said:
Juan,

Thanks for the info on that one.
 
J

Juan T. Llibre

Hi, Eagle.

That looks good.

Essentially, you are adding pragma no-cache and expires.now.toGMT
to the Cache-Contrl no-cache instruction, which widens the browser target.

Here's a caveat :

Instead of using
DateTime.Now.ToUniversalTime().ToLongDateString() +
" " + DateTime.Now.ToUniversalTime().ToLongTimeString() + " GMT";

Use a DateTime at least 25 hours in the past for your .Expires setting.
That will insure that your page will expire immediately in *all* timezones.

Using Now.ToUniversalTime means that some timezones will not
be at that time...yet, so the pages will not expire until they reach that time.

i.., if you're on the east coast of the US and use Now.ToUniversalTime,
the west coast's browsers' pages won't expire for another 3 hours.

For good measure, and simplicity, I use a 2-day-previous Expires setting when
I want to make sure that a page will expire no matter which timezone the client is in :

string MyExpire = DateTime.Now.AddDays(-2);
HtmlMeta meta = new HtmlMeta();
meta.Name = "expires";
meta.Content = MyExpire;
ctrl.Controls.Add(meta);

That should cover it.

If you'd like to, please test it...and let us know if it works well.
It's a little bit simpler datetime code, too... ;-)
 
J

Juan T. Llibre

Oops... that should have been :

DateTime DateTime1 = DateTime.Now.AddDays(-2);
String MyExpire = DateTime1.ToString();
HtmlMeta meta = new HtmlMeta();
meta.Name = "expires";
meta.Content = MyExpire;
ctrl.Controls.Add(meta);

You can tell I'm basically a VB.NET guy...

;-)
 
J

Joerg Jooss

Thus wrote (e-mail address removed),
Thanks for the responses. Sorry, I have not checked this for the last
few days.

I particularly like the Response.Cache.SetNoStore. I have seen
somewhere that setting "Cache-Control" to "no-cache" in the http
header does not work in all browsers.

Neither does setting No-Store. Caching and browsers histories are conceptually
not related, as far as the HTTP spec is concerned. Some browsers do implement
it like this, but all you'll ever get is a partially working solution.
I did work out a solution based upon a reference from our friend, Dino
Esposito, in his book from MSPress, Programming Microsoft ASP.NET 2.0,
on page 134. This addresses a way to add http-equiv metadata.
[...]

Note that Meta tags are useless when dealing with caching proxies.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top