Connecting Tech Pros Worldwide Help | Site Map

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

EagleRed@HighFlyingBirds.com
Guest
 
Posts: n/a
#1: Aug 2 '06
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



Alan Silver
Guest
 
Posts: n/a
#2: Aug 8 '06

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


In article <6C75AB00-EED7-4D91-8C0F-97EBACCDDCD3@microsoft.com>,
"EagleRed@HighFlyingBirds.com"
<EagleRedHighFlyingBirdscom@discussions.microsoft. comwrites
Quote:
>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.
Quote:
>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...
Quote:
>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)
Juan T. Llibre
Guest
 
Posts: n/a
#3: Aug 9 '06

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


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" <alan-silver@nospam.thanx.invalidwrote in message
news:YMcvi$3IkQ2EFwZq@nospamthankyou.spam...
Quote:
In article <6C75AB00-EED7-4D91-8C0F-97EBACCDDCD3@microsoft.com>, "EagleRed@HighFlyingBirds.com"
<EagleRedHighFlyingBirdscom@discussions.microsoft. comwrites
Quote:
>>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.
>
Quote:
>>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...
>
Quote:
>>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
Guest
 
Posts: n/a
#4: Aug 9 '06

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


In article <eml3F30uGHA.3912@TK2MSFTNGP03.phx.gbl>, Juan T. Llibre
<nomailreplies@nowhere.comwrites
Quote:
>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
Quote:
>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" <alan-silver@nospam.thanx.invalidwrote in message
>news:YMcvi$3IkQ2EFwZq@nospamthankyou.spam...
Quote:
>In article <6C75AB00-EED7-4D91-8C0F-97EBACCDDCD3@microsoft.com>,
>>"EagleRed@HighFlyingBirds.com"
><EagleRedHighFlyingBirdscom@discussions.microsoft .comwrites
Quote:
>>>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.
>>
Quote:
>>>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...
>>
Quote:
>>>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)
Juan T. Llibre
Guest
 
Posts: n/a
#5: Aug 9 '06

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


re:
Quote:
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" <alan-silver@nospam.thanx.invalidwrote in message
news:aa$PXnDFgb2EFwpn@nospamthankyou.spam...
Quote:
In article <eml3F30uGHA.3912@TK2MSFTNGP03.phx.gbl>, Juan T. Llibre <nomailreplies@nowhere.com>
writes
Quote:
>>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
>
Quote:
>>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" <alan-silver@nospam.thanx.invalidwrote in message
>>news:YMcvi$3IkQ2EFwZq@nospamthankyou.spam...
Quote:
>>In article <6C75AB00-EED7-4D91-8C0F-97EBACCDDCD3@microsoft.com>, "EagleRed@HighFlyingBirds.com"
>><EagleRedHighFlyingBirdscom@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
Guest
 
Posts: n/a
#6: Aug 9 '06

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


Juan,

Thanks for the info on that one.

In article <e1cuyC7uGHA.4160@TK2MSFTNGP06.phx.gbl>, Juan T. Llibre
<nomailreplies@nowhere.comwrites
Quote:
>re:
Quote:
>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" <alan-silver@nospam.thanx.invalidwrote in message
>news:aa$PXnDFgb2EFwpn@nospamthankyou.spam...
Quote:
>In article <eml3F30uGHA.3912@TK2MSFTNGP03.phx.gbl>, Juan T. Llibre
>><nomailreplies@nowhere.com>
>writes
Quote:
>>>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
>>
Quote:
>>>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" <alan-silver@nospam.thanx.invalidwrote in message
>>>news:YMcvi$3IkQ2EFwZq@nospamthankyou.spam...
>>>In article <6C75AB00-EED7-4D91-8C0F-97EBACCDDCD3@microsoft.com>,
>>>>"EagleRed@HighFlyingBirds.com"
>>><EagleRedHighFlyingBirdscom@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)
EagleRed@HighFlyingBirds.com
Guest
 
Posts: n/a
#7: Aug 9 '06

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


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:
Quote:
Juan,
>
Thanks for the info on that one.
>
In article <e1cuyC7uGHA.4160@TK2MSFTNGP06.phx.gbl>, Juan T. Llibre
<nomailreplies@nowhere.comwrites
Quote:
re:
Quote:
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" <alan-silver@nospam.thanx.invalidwrote in message
news:aa$PXnDFgb2EFwpn@nospamthankyou.spam...
Quote:
In article <eml3F30uGHA.3912@TK2MSFTNGP03.phx.gbl>, Juan T. Llibre
><nomailreplies@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" <alan-silver@nospam.thanx.invalidwrote in message
>>news:YMcvi$3IkQ2EFwZq@nospamthankyou.spam...
>>In article <6C75AB00-EED7-4D91-8C0F-97EBACCDDCD3@microsoft.com>,
>>>"EagleRed@HighFlyingBirds.com"
>><EagleRedHighFlyingBirdscom@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)
>
Juan T. Llibre
Guest
 
Posts: n/a
#8: Aug 9 '06

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


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/
===================================
"EagleRed@HighFlyingBirds.com" <EagleRedHighFlyingBirdscom@discussions.microsoft. comwrote in
message news:ACFA6804-CAA3-4AA3-AC95-AEEF3CB3DA7C@microsoft.com...
Quote:
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:
>
Quote:
>Juan,
>>
>Thanks for the info on that one.
>>
>In article <e1cuyC7uGHA.4160@TK2MSFTNGP06.phx.gbl>, Juan T. Llibre
><nomailreplies@nowhere.comwrites
Quote:
>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" <alan-silver@nospam.thanx.invalidwrote in message
>news:aa$PXnDFgb2EFwpn@nospamthankyou.spam...
>In article <eml3F30uGHA.3912@TK2MSFTNGP03.phx.gbl>, Juan T. Llibre
>><nomailreplies@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" <alan-silver@nospam.thanx.invalidwrote in message
>>>news:YMcvi$3IkQ2EFwZq@nospamthankyou.spam...
>>>In article <6C75AB00-EED7-4D91-8C0F-97EBACCDDCD3@microsoft.com>,
>>>>"EagleRed@HighFlyingBirds.com"
>>><EagleRedHighFlyingBirdscom@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)
>>

Juan T. Llibre
Guest
 
Posts: n/a
#9: Aug 9 '06

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


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" <nomailreplies@nowhere.comwrote in message
news:eqKDQ49uGHA.4296@TK2MSFTNGP06.phx.gbl...
Quote:
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/
===================================
"EagleRed@HighFlyingBirds.com" <EagleRedHighFlyingBirdscom@discussions.microsoft. comwrote in
message news:ACFA6804-CAA3-4AA3-AC95-AEEF3CB3DA7C@microsoft.com...
Quote:
>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:
>>
Quote:
>>Juan,
>>>
>>Thanks for the info on that one.
>>>
>>In article <e1cuyC7uGHA.4160@TK2MSFTNGP06.phx.gbl>, Juan T. Llibre
>><nomailreplies@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" <alan-silver@nospam.thanx.invalidwrote in message
>>news:aa$PXnDFgb2EFwpn@nospamthankyou.spam...
>>In article <eml3F30uGHA.3912@TK2MSFTNGP03.phx.gbl>, Juan T. Llibre
>>><nomailreplies@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" <alan-silver@nospam.thanx.invalidwrote in message
>>>>news:YMcvi$3IkQ2EFwZq@nospamthankyou.spam...
>>>>In article <6C75AB00-EED7-4D91-8C0F-97EBACCDDCD3@microsoft.com>,
>>>>>"EagleRed@HighFlyingBirds.com"
>>>><EagleRedHighFlyingBirdscom@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)
>>>
>
>

Joerg Jooss
Guest
 
Posts: n/a
#10: Aug 12 '06

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


Thus wrote EagleRedHighFlyingBirdscom@discussions.microsoft.c om,
Quote:
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.
Quote:
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
news-reply@joergjooss.de


Joerg Jooss
Guest
 
Posts: n/a
#11: Aug 12 '06

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


Thus wrote Juan,
Quote:
re:
>
Quote:
>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
news-reply@joergjooss.de


Juan T. Llibre
Guest
 
Posts: n/a
#12: Aug 12 '06

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


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" <news-reply@joergjooss.dewrote in message
news:94fc507140f8e8c88c1c6ec9da50@msnews.microsoft .com...
Quote:
Thus wrote Juan,
>
Quote:
>re:
>>
Quote:
>>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
news-reply@joergjooss.de
>
>

Closed Thread