Using If-Modified-Since Http Header

R

Roshawn Dawson

Hi,

I've been reading the <a href="http://www.google.com/webmasters/guidelines.html">Google WebMaster
Guidelines. Google urges web developers to make use of the If-Modified-Since http header with an
emphasis on saving bandwidth and overhead.

How do I use this header in ASP.NET? Is it there by default or must I explicitly place it in the
Headers collection? How would I inform the GoogleBot of this information?

Thanks,
Roshawn
 
J

Juan T. Llibre

Additional info at :

http://www.tutorialized.com/tutorial/Work-with-If-Modified-Since-and-Last-Modified-in-ASP.Net./9068

Here's sample code :

Dim dtNowUnc As DateTime = DateTime.Now().ToUniversalTime
Dim sDtModHdr = Request.Headers.Get("If-Modified-Since")
' does header contain If-Modified-Since?
If (sDtModHdr <> "") And IsDate(sDtModHdr) Then
' convert to UNC date
Dim dtModHdrUnc As DateTime = Convert.ToDateTime(sDtModHdr).ToUniversalTime
' if it was within the last 15 minutes, return 304 and exit
If DateTime.Compare(dtModHdrUnc, dtNowUnc.AddMinutes(-15)) > 0 Then
Response.StatusCode = 304
Response.End()
Exit Sub
End If
End If
' add Last-modified to header - FeedDemon stores this with cached
' feed so it's passed to the server the next time the feed is updated
Response.AddHeader("Last-modified", dtNowUnc.ToString("r"))

Picked up from :

http://nick.typepad.com/blog/2004/09/rss_bandwidth_c.html
 
R

Roshawn Dawson

Thanks Juan. Nice references!! :)
Additional info at :

http://www.tutorialized.com/tutorial/Work-with-If-Modified-Since-and-Last-Modified-in-ASP.Net./9068

Here's sample code :

Dim dtNowUnc As DateTime = DateTime.Now().ToUniversalTime
Dim sDtModHdr = Request.Headers.Get("If-Modified-Since")
' does header contain If-Modified-Since?
If (sDtModHdr <> "") And IsDate(sDtModHdr) Then
' convert to UNC date
Dim dtModHdrUnc As DateTime = Convert.ToDateTime(sDtModHdr).ToUniversalTime
' if it was within the last 15 minutes, return 304 and exit
If DateTime.Compare(dtModHdrUnc, dtNowUnc.AddMinutes(-15)) > 0 Then
Response.StatusCode = 304
Response.End()
Exit Sub
End If
End If
' add Last-modified to header - FeedDemon stores this with cached
' feed so it's passed to the server the next time the feed is updated
Response.AddHeader("Last-modified", dtNowUnc.ToString("r"))

Picked up from :

http://nick.typepad.com/blog/2004/09/rss_bandwidth_c.html
 
J

Joerg Jooss

Roshawn said:
Hi,

I've been reading the <a
href="http://www.google.com/webmasters/guidelines.html">Google
WebMaster Guidelines. Google urges web developers to make use of the
If-Modified-Since http header with an emphasis on saving bandwidth
and overhead.

How do I use this header in ASP.NET? Is it there by default or must
I explicitly place it in the Headers collection? How would I inform
the GoogleBot of this information?

Note that in addition to Juan's code-based approach where *you* do all
the work, there's also an easier way: Let infrastructure work its magic.

All you need to do is allow caching of your pages by applying the HTTP
headers "Cache-Control: max-age" and "Expires" (for HTTP 1.0
compatibility).

Setting these headets can be done in code, via page directives or by
configuring your web server to do it.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top