Is there a way that I can programmatically set an Expires Header in code with ASP.NET? Specifically I need to set it on an entire folder and all sub-folders, and the folder contains only static files (JavaSciprt, CSS, Images etc.) and not aspx files, so I can't just add some code to an aspx code-behind page_load.
I can normally set this directly in IIS. But the server is locked down by the client (I only have FTP access to web app directory for deployments), and getting the client to set the Expires Header on IIS would take an ice age (it's a public sector/government site).
I'm doing this for Front-End optimisation reasons as per Yahoo's recommendations http://developer.yahoo.com/performance/rules.html#expires
Update: I've tried creating an HttpModule...
public class FarFutureExpiresModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
string url = context.Request.Url.ToString();
if (url.Contains("/StaticContent/"))
{
context.Response.Cache.SetExpires(DateTime.Now.AddYears(30));
}
}
}
Although this doesn't see to work. I've placed a breakpoint on the code, and it appers to run correctly. However, when I analyse the raw HTTP header information in Firefox, the expires value is not being set. Notice I'm using BeginRequest, but I've also tried hooking into PostReleaseRequestState and PreSendRequestHeaders and they don't seem to work either. Any ideas?
Update 2: OK so it seems because I'm running IIS6, HttpModules won't run for static files, only dynamic files (*.aspx etc.). Thanks to RickNZ's help I came up with the following IHttpModule:
public class FarFutureExpiresModule : IHttpModule
{
public void Dispose() { }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
string url = context.Request.Url.ToString();
if (url.Contains("/StaticContent/"))
{
context.Response.Cache.SetExpires(DateTime.Now.AddYears(30));
context.Response.Cache.SetMaxAge(TimeSpan.FromDays(365.0 * 3.0));
}
}
}
...and it seems to work, but only in the built-in web server in Visual Studio, and in IIS7 (when in Intergrated Pipeline mode). A work colleague mentioned setting wildcard mappings on IIS6 to get HttpModules to work on static files, but if I have access to IIS6 I could just set the Far-Future Expires header directly and not bother with this HttpModule. Oh well!