HELP: How to force an aspx page to always be served vs. using cache...

Last post 07-04-2009 4:57 PM by celoftis. 4 replies.

Sort Posts:

  • HELP: How to force an aspx page to always be served vs. using cache...

    07-01-2009, 3:38 PM
    • Member
      53 point Member
    • celoftis
    • Member since 01-10-2007, 2:26 PM
    • Posts 306

    Using VS2005, VB code behind and javascript. 

    I have several pages that when accessed for a second or subsequent times always need to hit the server - but what I'm seeing with some users is that they are accessing cached pages. I want to force these pages to always get the latest page from the server.


    From client side javascript I know how to set the "if-modifed-since" request header to ensure that my xmlHttpRequest's are always served, i.e. "xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");". When I view the request headers collection from this request I see "If-Modified-Since" with a value of "Sat, 1 Jan 2000 00:00:00 GMT" in the list.


    My problem is that I want my aspx page requests - not just ones that originate from javascript - to have the "If-Modified-Since" request header set to an old date. Stated another way, I want all my pages to come back with the "If-Modified-Since" value in the Header's collection  set to some date (i.e. "Sat, 1 Jan 2000 00:00:00 GMT") that will ensure that the page gets served from the server vs. cache.


    Having javascript requests under control, I now need to do the same for all my aspx pages. Here is what I have tried in my code behind. 


        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Response.AddHeader("Last-Modified", "Sat, 1 Jan 2000 00:00:00 GMT")

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            Response.AddHeader("Last-Modified", "Sat, 1 Jan 2000 00:00:00 GMT")

            Response.AddHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")

        End Sub

    Neither of the options above produce the "If-Modified-Since" value in the request's header.


    Anyone have an idea what I am doing wrong?


  • Re: HELP: How to force an aspx page to always be served vs. using cache...

    07-01-2009, 4:26 PM
    • Contributor
      3,583 point Contributor
    • mreyeros
    • Member since 12-05-2002, 10:32 AM
    • Miami, FL USA
    • Posts 647

    Good afternoon celoftis, try adding the following in your pageload() event:

    Response.AppendHeader("Cache-Control", "no-store");


     

    --------------------------------------------------
    Sincerely,
    Michael Reyeros

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    If my solution doesn't solve your problem, just feel free to mark it as not answer and reply.
  • Re: HELP: How to force an aspx page to always be served vs. using cache...

    07-01-2009, 7:51 PM
    • Star
      11,863 point Star
    • shahed.kazi
    • Member since 07-09-2008, 2:15 AM
    • Sydney, Australia
    • Posts 2,330

    You can use meta tags.

    <META HTTP-EQUIV="Pragma" CONTENT="no-cache">

  • Re: HELP: How to force an aspx page to always be served vs. using cache...

    07-02-2009, 12:58 PM
    • Member
      53 point Member
    • celoftis
    • Member since 01-10-2007, 2:26 PM
    • Posts 306

    What I did was change all my aspx pages to extend from a common class (BasePage). Then in the OnLoadComplete event in the BasePage, I added all kinds of Response (see below) - but when I access these pages then check my trace.axd for the Request, I don't see anything in the Headers collection or the server variables (ALL_HTTP, ALL_RAW, etc.) that leads me to believe that the page isn't expired in cache. How can I verify that the page isn't being cached but downloaded each time?


    "BasePage" code below:

    Public Class BasePage

        Inherits System.Web.UI.Page

        Protected Overrides Sub OnLoadComplete(ByVal e As System.EventArgs)

            If Page.IsPostBack Then

            Else

                Response.AppendHeader("Pragma", "no-cache")

                Response.AppendHeader("Cache-Control", "no-store");  

                Response.Cache.SetNoStore()

                Response.Cache.SetExpires(DateTime.Parse(DateTime.Now.ToString()))

                Response.Cache.SetCacheability(HttpCacheability.NoCache)

                Response.Expires = -1

            End If

            MyBase.OnLoadComplete(e)

        End Sub

    End Class

    Imports Microsoft.VisualBasic
    Public Class BasePage
        Inherits System.Web.UI.Page
        Protected Overrides Sub OnLoadComplete(ByVal e As System.EventArgs)
            If Page.IsPostBack Then
            Else
                'Response.AddHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT")
                'Response.AddHeader("Last-Modified", "Sat, 1 Jan 2000 00:00:00 GMT")
                Response.AppendHeader("Pragma", "no-cache")
                Response.AppendHeader("Cache-Control", "no-store");  
                Response.Cache.SetNoStore()
                Response.Cache.SetExpires(DateTime.Parse(DateTime.Now.ToString()))
                Response.Cache.SetCacheability(HttpCacheability.NoCache)
                Response.Expires = -1
            End If
            MyBase.OnLoadComplete(e)
        End Sub
    End Class



  • Re: HELP: How to force an aspx page to always be served vs. using cache...

    07-04-2009, 4:57 PM
    Answer
    • Member
      53 point Member
    • celoftis
    • Member since 01-10-2007, 2:26 PM
    • Posts 306

    I believe the code above worked. To verify I downloaded Fiddler2 and watched all the requests and responses come back with code 200 or 302. Thanks for the suggestions.

Page 1 of 1 (5 items)