Do not cache pages in ASP

Source: Internet
Author: User
Tags iis urlencode
Cache | page | cache Update 1/8/2004
At the top of the page:

<%
PSTR = "Private, No-cache, Must-revalidate"
Response.ExpiresAbsolute = #2000 -01-01#
Response.AddHeader "Pragma", "No-cache"
Response.AddHeader "Cache-control", pstr
%>

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

Don ' t allow your page to be cached. Why? You are might want to prevent the users from seeing the old content. Insert the following code in the <HEAD> tag of your document.

<meta http-equiv= "Expires" content= "Tue, 04-dec-1993 21:29:02 GMT" >

Cache No More (ASP)
by Phil Paxton
(Phil@matchw.com)

Here are the "things dealing with" issue of caching in ASP:

1. Response.Expires = 0
2. Response.ExpiresAbsolute = Now ()-1
3. Response.AddHeader "Cache-control", "private"
4. Response.AddHeader "Pragma", "No-cache"
5. Adding a "cachebuster" by creating a unique URL.

Notes:
#1 is said to expire at seconds, not 0. Also, Khin Zaw (from activeserverpages@ and aspadvanced@) has posted the "a" and "time spent" with some IIS internals expe RTS revealing this can is a very touchy parameter to rely upon and usually requires a rather "large" negative number or PE Dantically, that's would be a very small number).

#2 (my own creation) says "expire this page hours ago", allowing to time differences, rather than specify a static dat E.

#3, #4 from a MS KB article. The code is correct but there are some incorrect statements in the article.

n.b. Some related KB articles include:
(Info:controlling the Caching of Web Pages with IIS 4.0)
(Prb:browser doesn ' t show Most recent versions of htm/asp Files)
(Pragma:no-cache with IIS and IE)

#5 my term, but not my technique. IE 5.0 can defeat #1-#4 used in conjunction but adding #5 'll break it. I usually use something like "? Nocache=rnd "After a statement." Bill Wilkinson (of chili! Soft) has proposed an alternate of Nocache=server.urlencode (now ()).


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

Another thing to Remember:netscape'll continue to cache, even if your turn all caching off. This is behavior persisted through 4.5 PR1, PR2, and now in the released version of 4.5.

If you fear your might have to deal with caching later, you are might want to build contingencies into your app as your go. Retrofitting #5 throughout even a medium-sized app would take a rather sizeable. Could retrofit #1-#4 (inclusive) rather quickly with a/single pass through the application, but #5 takes a lot of extr A effort. And to So, I don ' t ever Response.Redirect anywhere in my Code except sample code I post to the lists (then again, th E Only time I use Response.Write be to the "list because I rely on my utilities-form.inc library for Display () and HTML (). Everything is Redirect (Newurl) where the Redirect function looks as this:


Function Redirect (Newurl)
'
If Not IsEmpty (Newurl & "") Then
Dim Questionmark
'
Questionmark = Instr (Newurl, "?")
'
If questionmark = 0 Then
Response.Redirect Newurl & "?" & Nocacheurl ()
Response.End
Else
Response.Redirect Newurl & "&" & Nocacheurl ()
Response.End
End If
End If
'
End Function
And Nocacheurl looks like this:


Function Nocacheurl ()
'
On Error Resume Next
'
Randomize
' Randomize not needed if ' ()
'
Nocacheurl = "nocache=" & Server.URLEncode (RND)
'
' or Nocacheurl = ' nocache= ' & Server.URLEncode (now ())
' Per Bill
'
End Function



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.