By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,025 Members | 2,118 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,025 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 <headelement. 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

Aug 2 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
In article <6C**********************************@microsoft.co m>,
"Ea******@HighFlyingBirds.com"
<Ea************************@discussions.microsoft. comwrites
>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 <headelement. 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 <headin 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

--
Alan Silver
(anything added below this line is nothing to do with me)
Aug 8 '06 #2

P: n/a
I prefer to use :

Response.Cache.SetNoStore();

....instead of using meta tags.


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Alan Silver" <al*********@nospam.thanx.invalidwrote in message
news:YM**************@nospamthankyou.spam...
In article <6C**********************************@microsoft.co m>, "Ea******@HighFlyingBirds.com"
<Ea************************@discussions.microsoft. comwrites
>>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 <headelement. 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 <headin 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

--
Alan Silver
(anything added below this line is nothing to do with me)

Aug 9 '06 #3

P: n/a
In article <em**************@TK2MSFTNGP03.phx.gbl>, Juan T. Llibre
<no***********@nowhere.comwrites
>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
>Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Alan Silver" <al*********@nospam.thanx.invalidwrote in message
news:YM**************@nospamthankyou.spam...
>In article <6C**********************************@microsoft.co m>,
"Ea******@HighFlyingBirds.com"
<Ea************************@discussions.microsoft .comwrites
>>>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 <headelement. 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 <headin 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

--
Alan Silver
(anything added below this line is nothing to do with me)

--
Alan Silver
(anything added below this line is nothing to do with me)
Aug 9 '06 #4

P: n/a
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/lib...etnostore.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)


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

"Alan Silver" <al*********@nospam.thanx.invalidwrote in message
news:aa**************@nospamthankyou.spam...
In article <em**************@TK2MSFTNGP03.phx.gbl>, Juan T. Llibre <no***********@nowhere.com>
writes
>>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
>>Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Alan Silver" <al*********@nospam.thanx.invalidwrote in message
news:YM**************@nospamthankyou.spam...
>>In article <6C**********************************@microsoft.co m>, "Ea******@HighFlyingBirds.com"
<Ea************************@discussions.microsof t.comwrites
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 <headelement. 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 <headin 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

--
Alan Silver
(anything added below this line is nothing to do with me)


--
Alan Silver
(anything added below this line is nothing to do with me)


Aug 9 '06 #5

P: n/a
Juan,

Thanks for the info on that one.

In article <e1**************@TK2MSFTNGP06.phx.gbl>, Juan T. Llibre
<no***********@nowhere.comwrites
>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/lib...chepolicy.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)


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

"Alan Silver" <al*********@nospam.thanx.invalidwrote in message
news:aa**************@nospamthankyou.spam...
>In article <em**************@TK2MSFTNGP03.phx.gbl>, Juan T. Llibre
<no***********@nowhere.com>
writes
>>>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
>>>Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Alan Silver" <al*********@nospam.thanx.invalidwrote in message
news:YM**************@nospamthankyou.spam...
In article <6C**********************************@microsoft.co m>,
"Ea******@HighFlyingBirds.com"
<Ea************************@discussions.microso ft.comwrites
>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 <headelement. 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 <headin 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

--
Alan Silver
(anything added below this line is nothing to do with me)


--
Alan Silver
(anything added below this line is nothing to do with me)


--
Alan Silver
(anything added below this line is nothing to do with me)
Aug 9 '06 #6

P: n/a
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" wrote:
Juan,

Thanks for the info on that one.

In article <e1**************@TK2MSFTNGP06.phx.gbl>, Juan T. Llibre
<no***********@nowhere.comwrites
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/lib...chepolicy.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)


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

"Alan Silver" <al*********@nospam.thanx.invalidwrote in message
news:aa**************@nospamthankyou.spam...
In article <em**************@TK2MSFTNGP03.phx.gbl>, Juan T. Llibre
<no***********@nowhere.com>
writes
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

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Alan Silver" <al*********@nospam.thanx.invalidwrote in message
news:YM**************@nospamthankyou.spam...
In article <6C**********************************@microsoft.co m>,
"Ea******@HighFlyingBirds.com"
<Ea************************@discussions.microsof t.comwrites
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 <headelement. 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 <headin 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

--
Alan Silver
(anything added below this line is nothing to do with me)

--
Alan Silver
(anything added below this line is nothing to do with me)

--
Alan Silver
(anything added below this line is nothing to do with me)
Aug 9 '06 #7

P: n/a
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... ;-)


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Ea******@HighFlyingBirds.com" <Ea************************@discussions.microsoft. comwrote in
message news:AC**********************************@microsof t.com...
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" wrote:
>Juan,

Thanks for the info on that one.

In article <e1**************@TK2MSFTNGP06.phx.gbl>, Juan T. Llibre
<no***********@nowhere.comwrites
>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/lib...chepolicy.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)


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

"Alan Silver" <al*********@nospam.thanx.invalidwrote in message
news:aa**************@nospamthankyou.spam...
In article <em**************@TK2MSFTNGP03.phx.gbl>, Juan T. Llibre
<no***********@nowhere.com>
writes
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

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Alan Silver" <al*********@nospam.thanx.invalidwrote in message
news:YM**************@nospamthankyou.spam...
In article <6C**********************************@microsoft.co m>,
"Ea******@HighFlyingBirds.com"
<Ea************************@discussions.microso ft.comwrites
>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 <headelement. 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 <headin 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

--
Alan Silver
(anything added below this line is nothing to do with me)

--
Alan Silver
(anything added below this line is nothing to do with me)

--
Alan Silver
(anything added below this line is nothing to do with me)

Aug 9 '06 #8

P: n/a
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...

;-)


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:eq**************@TK2MSFTNGP06.phx.gbl...
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... ;-)


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Ea******@HighFlyingBirds.com" <Ea************************@discussions.microsoft. comwrote in
message news:AC**********************************@microsof t.com...
>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" wrote:
>>Juan,

Thanks for the info on that one.

In article <e1**************@TK2MSFTNGP06.phx.gbl>, Juan T. Llibre
<no***********@nowhere.comwrites
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/lib...chepolicy.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)


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================

"Alan Silver" <al*********@nospam.thanx.invalidwrote in message
news:aa**************@nospamthankyou.spam...
In article <em**************@TK2MSFTNGP03.phx.gbl>, Juan T. Llibre
<no***********@nowhere.com>
writes
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

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Alan Silver" <al*********@nospam.thanx.invalidwrote in message
news:YM**************@nospamthankyou.spam...
In article <6C**********************************@microsoft.co m>,
>"Ea******@HighFlyingBirds.com"
<Ea************************@discussions.micros oft.comwrites
>>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 <headelement. 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 <headin 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
>
--
Alan Silver
(anything added below this line is nothing to do with me)

--
Alan Silver
(anything added below this line is nothing to do with me)


--
Alan Silver
(anything added below this line is nothing to do with me)


Aug 9 '06 #9

P: n/a
Thus wrote Ea************************@discussions.microsoft.c om,
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,
--
Joerg Jooss
ne********@joergjooss.de
Aug 12 '06 #10

P: n/a
Thus wrote Juan,
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/lib...cachepolicy.se
tnostore.aspx
Actually, this sets the Cache-Control: No-Store header, which is an entirely
different beast than No-Cache ;-)

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
Aug 12 '06 #11

P: n/a
Oops, you're right.

I meant HttpCachePolicy.SetCacheability
http://msdn2.microsoft.com/en-us/lib...heability.aspx
http://msdn2.microsoft.com/en-us/lib...heability.aspx

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Joerg Jooss" <ne********@joergjooss.dewrote in message
news:94**************************@msnews.microsoft .com...
Thus wrote Juan,
>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/lib...cachepolicy.se
tnostore.aspx

Actually, this sets the Cache-Control: No-Store header, which is an entirely different beast than
No-Cache ;-)

Cheers, --
Joerg Jooss
ne********@joergjooss.de


Aug 12 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.