Owen Blacker
1/30/2006 4:32:00 PM
I'm having some trouble sorting out image caching for JPEGs that are
served through an IHttpModule.
A quirk of the site I'm working on at the moment is that product images
are served via a third-party image hosting provider (Scene7, who have
been very helpful and so deserve a credit). In order to save having to
reference the URI stem all over our code, I wrote an IHttpModule that
parses all requests on the basis of:
HttpContext context = ((HttpApplication) sender).Context;
HttpRequest request = context.Request;
Regex r = new Regex(
@"/images/products/(?<sku>[A-Z0-9-]+)_(?<size>\d)\.jpg",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
Match m = r.Match(request.RawUrl);
if (m.Success && request.HttpMethod == "GET")
{
// Handle the Scene7-specific stuff
}
else
{
// Set cacheability
}
Before I was doing anything in this else clause, Fiddler
(www.fiddlertool.com) was showing me that the following headers were
being sent with all non-Scene7 JPEGs:
Pragma: no-cache
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Now, if I add the following two lines to my else clause:
HttpCachePolicy cache = context.Response.Cache;
cache.SetAllowResponseInBrowserHistory(true);
it gets rid of the "Expires: -1" header. Adding any / all of the
following, though, won't shift the Pragma: and Cache-Control: headers
-- they all seem to have no effect on my actual HTTP transaction, so
it's still not being cached properly:
cache.SetCacheability(HttpCacheability.Public);
cache.SetMaxAge(new TimeSpan(30, 0, 0, 0));
cache.SetExpires(DateTime.Now.AddDays(30));
cache.SetLastModifiedFromFileDependencies();
cache.SetValidUntilExpires(true);
Does anyone have any experience with setting HttpCachePolicy properties
from within an IHttpModule? Am I missing something really obvious?
If people could try to remember to cc: me on replies, as well as
sending them to this newsgroup, as I don't always remember to check
Usenet as often as I'd like.
Thanks everyone!
Owen
--
Owen Blacker
Senior Software Developer
Wheel: insight | thinking | creativity
owen.blacker@wheel.co.uk www.wheel.co.uk