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

Posted on 2012-09-16
Medium Priority
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
  • 3
  • 2
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38404002
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

ID: 38404040
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 84

Expert Comment

by:Dave Baldwin
ID: 38404079
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

ID: 38404147
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 84

Accepted Solution

Dave Baldwin earned 2000 total points
ID: 38404430
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.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 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