views:

144

answers:

2

I have a system which sits on a web server and generates files on the fly in response to HTTP requests. This is currently implemented as an HTTPHandler.

Once the files are generated, they don't change very often, so I'd like to implement a cache.

Ideally, I'd like the web server to look at the cache folder and serve the files directly from there without any of my code having to execute (web servers are designed to be good at serving files after all, so if I can keep out of the way of that so much the better!).

What I'd like to then do is hook into the server's "file not found" event as an opportunity to create the file, drop a copy in the cache folder for the next time it's requested and also return it to the user instead of the "file not found" message.

This way, repeat requests for files will be lightening fast and my code will only get called in 'exceptional' cases.

So - the question is - how do I wire my code into the "file not found" event in as unobtrusive and lightweight way as possible?

Thanks

+1  A: 

How about redirecting to your HttpHandler using the customError configuration.

<customErrors mode="On">
   <error statusCode="404" redirect="FileGeneratorHandler.ashx" />
</customErrors>

The only issue would be whether the referring page is available to the handler.

tvanfosson
+2  A: 

It's actually really simple, just point your 404 error page in IIS to your HTTPHandler.

Request.RawUrl will look like:

http://yourdomain.com/yourhandler.ashx;originally/requested/url
FlySwat
That's embarrassingly simple! Thanks - I'll give it a try!
Chris Roberts