views:

563

answers:

4

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!

+2  A: 

If you're using IIS 7, the easiest way to do it would be to write an HttpModule that runs for static files in Integrated mode, and set the Expires and Cache-Control headers from there.

Update:

Your HttpModule should work, although I normally also call:

context.Response.Cache.SetMaxAge(TimeSpan.FromDays(365.));

Update 2:

With IIS 6, you would have to programmatically modify the metabase. It's possible, although it requires elevated permissions.

The only other option would be to write an ISAPI module in C++.

RickNZ
Running IIS6 here unfortunately. But I've tried creating an HttpModule but can't seem to get it to work, see update above.
Sunday Ironfoot
Ah, too bad. HttpModules run for static files in Cassini, but not in IIS6; it requires Integrated Mode to work correctly.
RickNZ
Aww man! I just noticed my HttpModules don't seem to run for static files (but they do in the built Visual Studio Web Server). The above code worked a treat BTW, thanks, though sadly not in IIS6/7
Sunday Ironfoot
+1  A: 

You can write an HttpModule to handle that.

Oded
A: 

Good ways of improving performance include: gzip-compressing responses (not the easiest with IIS6), minifying static files (css, js), merging static files together (one big css, one big js), using sprites. The idea is to reduce the total number of HTTP requests and then to reduce the size of the responses.

Justice
GZipping and Far-future headers give you the biggest impact for mininal effort, but they're only possible if the client gives you access to the web server.
Sunday Ironfoot
A: 

context.Response.Cache.SetExpires(DateTime.Now.AddYears(30));

..Still not sure how to do this.. maybee need to look at the Response.Headers?

This wont work...

See disasembly of this method:

public void SetExpires(DateTime date) { DateTime time = DateTimeUtil.ConvertToUniversalTime(date); DateTime utcNow = DateTime.UtcNow; *if ((time - utcNow) > s_oneYear) { time = utcNow + s_oneYear; }* if (!this._isExpiresSet || (time < this._utcExpires)) { this.Dirtied(); this._utcExpires = time; this._isExpiresSet = true; } }

felickz