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

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

That's embarrassingly simple! Thanks - I'll give it a try!
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.