Get last-modified header from response in ASP.NET

May 21, 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:

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.

* $4.95/month ASP.NET Hosting with FREE SQL 2012 DB! – Click Here!

Comments (10) -

gOODiDEA
gOODiDEA United States
5/22/2009 6:56:03 PM #

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;
    }

Mads Kristensen
Mads Kristensen Denmark
5/23/2009 4:32:08 PM #

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.

Melayu Boleh
Melayu Boleh United States
5/26/2009 12:58:34 AM #

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

codeclue
codeclue United States
5/29/2009 4:43:08 AM #

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

9yr
9yr United States
6/1/2009 6:44:23 PM #

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

Sam
Sam United States
6/2/2009 3:12:12 AM #

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.

tummy tuck surgery
tummy tuck surgery United States
6/6/2009 5:43:31 AM #

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

Chris Eargle
Chris Eargle United States
6/7/2009 8:10:34 PM #

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.

AB Web Design, LLC
AB Web Design, LLC United States
7/23/2009 10:23:12 PM #

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

Abdul
Abdul Australia
7/26/2009 10:13:45 AM #

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

Pingbacks and trackbacks (5)+

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.