Using If-Modified-Since Http Header

  • Thread starter Thread starter Roshawn Dawson
  • Start date Start date
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
 
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



Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
 
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



Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
 
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,
 
Back
Top