Get last-modified header from response in ASP.NET

by Mads Kristensen 21. May 2009 18:30

I’ve been working lately with some ASP.NET performance optimization automation HTTP modules. In one of them I needed to know if the last-modified header had been set through the Response.Cache.SetLastModified(DateTime) method. For some reason, there is no API available anywhere within the BCL to retrieve the last modified date of a response – you can only set it.

Since the module wouldn’t work without a way to read the last modified date of the response, I had to use Reflector to figure out how to pull the information out using reflection. The result became a simple little method to retrieve the date. It looks like this:

private static DateTime ReadLastModifiedFromResponse(HttpCachePolicy cache)

{

  BindingFlags flags = BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance;

  return (DateTime)cache.GetType().GetField("_utcLastModified", flags).GetValue(cache);

}

And you can use it like this:

DateTime responseModified = ReadLastModifiedFromResponse(Response.Cache);

 

if (responseModified > DateTime.MinValue)

{

  // Last-modified is set. Do something...

}

If you know of another way to retrieving the last-modified date, please let me know.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Tags: , ,

ASP.NET | Tips and tricks

Comments

5/21/2009 9:57:04 PM #

trackback

Get last-modified header from response in ASP.NET

Thank you for submitting this cool story - Trackback from DotNetShoutout

DotNetShoutout |

5/21/2009 9:58:03 PM #

trackback

Get last-modified header from response in ASP.NET

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com |

5/23/2009 3:56:03 AM #

gOODiDEA

try

    public static bool IsClientCached( HttpContext context, DateTime contentModified )
    {
      string clientCached = context.Request.Headers ["If-Modified-Since"];
      if ( clientCached != null )
      {
        DateTime isModifiedSince;
        if ( DateTime.TryParse ( clientCached, out isModifiedSince ) )
        {
          return isModifiedSince > contentModified;
        }
      }
      return false;
    }

gOODiDEA United States |

5/24/2009 1:32:08 AM #

Mads Kristensen

Your method looks in the browser's cache instead of the actual response. It's good for conditional GETs, but it doesn't examine the actual response to retrieve the last-modified date.

Mads Kristensen Denmark |

5/26/2009 9:58:34 AM #

Melayu Boleh

i want to test your method. It look interesting. i will share here again after i test it.

Melayu Boleh United States |

5/29/2009 1:43:08 PM #

codeclue

just tried its and its working,, i dont know if there is other ways but i think there is

codeclue United States |

6/2/2009 3:44:23 AM #

9yr

i have tried the method but its not working, i dont know what the problem is

9yr United States |

6/2/2009 12:12:12 PM #

Sam

Have you looked at HttpWorkerRequest? Sometimes you can use this for low-level stuff. It has a GetKnownRequestHeader method. It also does some stuff with the response. I use it for getting and setting headers a lot.

Sam United States |

6/6/2009 2:43:31 PM #

tummy tuck surgery

Sam, HttpWorkerRequest is a bit more complicated stuff. I like more a variant of M. Kristensen. Cute and goodworking.

tummy tuck surgery United States |

6/8/2009 5:10:34 AM #

Chris Eargle

I don't see a better way to get it. I guess it's a good thing that reflection is available to break encapsulation when necessary.

Chris Eargle United States |

6/19/2009 5:37:05 PM #

pingback

Pingback from webproasp.com

Web Pro ASP - Active Server Page Development News

webproasp.com |

7/24/2009 7:23:12 AM #

AB Web Design, LLC

Thanks Mad,

I was actually looking for a way to accomplish this a while back...never did find an easy way to do it. I will try this method out.

Chris

AB Web Design, LLC United States |

7/26/2009 7:13:45 PM #

Abdul

Use of reflection to pull out this info is very quirky.. would lov to see more on this

Abdul Australia |

9/15/2009 6:54:04 PM #

pingback

Pingback from random.cgi-biz.com

ASP.NET -   Get last modified header from response in ASP.NET - Random Things To Blog

random.cgi-biz.com |

3/15/2010 11:14:11 PM #

pingback

Pingback from devwebpro.com

ASP.NET Performance Optimization Automation | WebProASP

devwebpro.com |

Comments are closed

About the slave

Mads Kristensen Mads Kristensen
Web developer at ZYB and founder of BlogEngine.NET. More...

LinkedIn ZYB Facebook Last.fm Twitter View Mads Kristensen's profile on Technorati

The Lounge

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008