ASP.NET / VB: 304 Redirect Should Expire Every Minute

The code below causes a 304 Redirect to be sent for browser caching and expires every week.  How can I get it to expire every minute instead of every week?  If someone hits the refresh key several times in a row, it should send a 304 header until a new minute begins.  

This code works but it only refreshes the page every week instead of every minute.

<%@ Page Language="VB" AutoEventWireup="false" %>

    Dim T1970 As New DateTime(1970, 1, 1)
    Dim Timestamp As Integer = (DateTime.Now.ToUniversalTime - T1970).TotalSeconds
    Dim firstDayThisWeek As Date = Today.AddDays(Today.DayOfWeek)
    Dim Life304 As New Date(firstDayThisWeek.Year, firstDayThisWeek.Month, firstDayThisWeek.Day, 0, 0, 1)
    Dim Life304Offset As Integer = (DateTime.Now.ToUniversalTime - Life304).TotalSeconds
    Dim TimestampWeek As Integer =  Timestamp - Life304Offset - 518400
	If Not Request.Headers("If-Modified-Since") Is Nothing Then
	 Dim tSince As String = Request.Headers("If-Modified-Since")
	 Dim t As Integer = System.Text.RegularExpressions.Regex.Replace(tSince, "[^0-9+]", "")
	 If Timestamp< (t + 604800) Then
	  Response.StatusCode = 304
	  Response.StatusDescription = "Not Modified"
         End If
        End If
	 Response.AppendHeader("Pragma", "public")
	 Response.AppendHeader("Connection", "close")
	 Response.AppendHeader("Last-Modified", TimestampWeek & " GMT")
	 Response.AppendHeader("ETag", "Dyno")

Response.Write("Hello World")


Open in new window

LVL 16
Who is Participating?
Dave BaldwinFixer of ProblemsCommented:
So you want to put this at the top of the ASP.NET page, set an arbitrary expiration, and then not send any content until it expires?  The first thing to do would be to get rid of the day-of-the-week code because it's interfering.

Also note that if I press Ctl-F5, the "If-Modified-Since" line is not sent so you have nothing to compare against.
Dave BaldwinFixer of ProblemsCommented:
518400 in line 9 is 6 days in seconds.  604800 in line 13 is 7days in seconds.  You could try making both of them 60 which is 60 seconds / 1 minute.  Note that that defeats the purpose of a 304 code.  In general you want a long expiration on things that don't change because it speeds up the display of a page if the browser gets the file from it's cache rather than getting a new copy from the server.
hankknightAuthor Commented:
Dave, thanks for your response.  Your idea of changing those two numbers to 60 does not work.  

The reason that it does not work is because it is calculating the first day of this week
in line 7:
Dim Life304 As New Date(firstDayThisWeek.Year, firstDayThisWeek.Month, firstDayThisWeek.Day, 0, 0, 1)

Open in new window

If you test the code you will see the problem.

I understand that generally speaking one minute is a short period of time to for a browser to cache an asset.

However, the content of this site changes every few minutes and many people refresh the page every few seconds waiting for new content, putting a heavy load on the database.

I would like to make it so if people hit their refresh key every few seconds, they will only get fresh content once per minute.

Dave BaldwinFixer of ProblemsCommented:
Is there some reason the normal 304 response isn't working on your web server?  If your content changes every 3 minutes, your web server should return "Not Modified" for about 2 minutes and 59 seconds of those 3 minutes.  You could have the expiration date set for 3 years and that should still work.  A short timeout does not change that.
hankknightAuthor Commented:
This is for a database driven ASP.NET script so I want to manage the 304 with more control than I get from IIS.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.