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

Posted on 2012-09-16
Last Modified: 2012-10-08
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

Question by:hankknight
    LVL 82

    Expert Comment

    by:Dave Baldwin
    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.
    LVL 16

    Author Comment

    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.

    LVL 82

    Expert Comment

    by:Dave Baldwin
    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.
    LVL 16

    Author Comment

    This is for a database driven ASP.NET script so I want to manage the 304 with more control than I get from IIS.
    LVL 82

    Accepted Solution

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now