Mads Kristensen

ASP.NET Performance Optimization Automation

   Follow me on Twitter:   Mads Kristensen   June 19th, 2009

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:

privatestaticDateTime 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.

Comments

About The Author

Mads Kristensen currently works as a Senior Developer at Traceworks located in Copenhagen, Denmark. Mads graduated from Copenhagen Technical Academy with a multimedia degree in 2003, but has been a professional developer since 2000. His main focus is on ASP.NET but is responsible for Winforms, Windows- and web services in his daily work as well. A true .NET developer with great passion for the simple solution. http://www.madskristensen.dk/