The continuing saga of StaticFileHandler - meet DefaultHttpHandler

by Matt 9. October 2006 16:32

I bloody well love Reflector. It's just magic.

After discovering that StaticFileHandler doesn't handle conditional gets and ranges, I've dug a little deeper, and, unsurprisingly, the plot thickens.

In .net 1.x, you could use StaticFileHandler to serve static files from an asp.net application - just use the IIS wildcard mapping so that all files went through the asp.net ISAPI dll, and modify your web.config.

In .net 2, things are a little better. Now, the global web.config file catches any GET, HEAD or POST requests to any file that hasn't already been handled and passes them off to DefaultHttpHandler, not StaticFileHandler. (Any other verbs get passed to HttpMethodNotAllowedHandler. That one's internal. 3 guesses what it does?)

This is quite a nice class, at least in theory. It's an asynchronous http handler that will serve static files. The bad news is that it serves static files using StaticFileHandler, with all the issues that has (no support for conditional gets or ranges). But the good news is that if you haven't modified any headers (such as caching) and you haven't already written something to the response stream, and the response stream doesn't have a filter (such as compression) and you're running in process in IIS6 then the request is passed back to IIS for processing. IIS will do the decent thing, and serve the static file with support for conditional gets and ranges and all will be good. If any of that exhaustive list is not true, then StaticFileHandler gets to serve the content (boo, hiss!). Note that you can still get compression, but you'll have to configure it at the IIS level now, not the HttpModule level. That's the good news.

This might seem of limited use - if you can't modify the headers, or add a compression filter to the output stream, what's the point? Well, you can monitor and log the request, for one thing. And more importantly, you can derive from the public DefaultHttpHandler and override the virtual OverrideExecuteUrlPath to route you off to somewhere else and IIS will serve it (sounds just like I was trying to do originally!)

Unfortunately, this class has it's problems, too. It doesn't allow you to route to your own static file handler. Even worse, though, is that you can only specify an override of the path when it decides (via conditions outside of your derived class's control) that IIS should serve the file. If it decides that StaticFileHandler should serve it, you have to have implicit knowledge of the inner workings of the DefaultHttpHandler class and know to do a Context.RewritePath in OnExecuteUrlPreconditionFailure. This is mentioned (albeit obliquely) in the docs for OverrideExecuteUrlPath. And I can't roll my own version of this as it uses internal methods to pass control back to IIS.

As long as you're strict about your global setup, this might be useful. I'll have to do a test to see if it'll fit my requirements, but this feels a little fragile - I really don't want it falling back to StaticFileHandler. I think I'll follow up on the Virtual Path Provider idea, or just buckle down and write a BetterStaticFileHandler.

Tags:

asp.net

Comments (24) -

increasing height
increasing height United States
6/24/2009 9:35:51 AM #

This is a great way to selectively enable compression on some requests. My solution turns on compression for all requests. This way you don't need to include the filter attribute on all action methods.

Reply

Janie Koka
Janie Koka
3/24/2011 12:47:08 PM #

The magnonomous truth about this post is, that it spoke to me deeply. Thank you for sharing your thoughts and concerns.

Reply

Houston Chiropractor
Houston Chiropractor
3/24/2011 2:59:47 PM #

The great truth about this  blog article is, that it spoke to me greatly. Thank you for sharing your thoughts and concerns.

Reply

Brittanie Ricardo
Brittanie Ricardo
5/8/2011 5:38:25 PM #

I should admit that this can be one great insight. It surely provides a company the opportunity to urge in on the bottom floor and extremely participate in making one thing special and tailored to their wants.

Reply

LAURENCE  Debora
LAURENCE Debora
5/22/2011 4:55:51 AM #

Recherche thématique sur plusieurs thèmes, c'est sukoga.com, moteur.

Reply

LAURENCE  Debora
LAURENCE Debora
5/27/2011 8:31:50 AM #

Liste d'annuaires détaillées pour faciliter le référencement, Créez votre propre article.

Reply

best suv 2011
best suv 2011
7/20/2011 10:20:21 PM #

it ti permette di orientarti con semplicità nel pulviscolare niverso di Moncler.Troverai le indicazioni per lo spaccio o negozio Moncler più vicino a casa tua felpe moncler, pantaloni e abbigliamento sportivo.

Reply

best hybrid cars
best hybrid cars
7/22/2011 4:28:06 AM #

I wonder if he cheated on her? I remember he cheated on his previous wife with JLO so it wouldn’t be surprising.

Reply

lifequotes
lifequotes
7/22/2011 12:45:15 PM #

Una pagina sarà dedicata agli accessori, una alle giacche e ai giubbotti. Troverai le indicazioni per lo spaccio o negozio Moncler più vicino a casa tua e tutte le offerte più vantaggiose di questo prestigioso marchio.

Reply

cheap nike air max uk
cheap nike air max uk
7/22/2011 9:23:09 PM #

In today's stage of social development, the topic about "serve others" is worth discussing.

Reply

iphone app development
iphone app development
7/24/2011 6:06:43 AM #

Hi,what an excellent article this is,I found it on bing and I like it very much,I agree with what you have said, lots of things will be learned form your site,but I still have some questions with the last part,can you explain it for me ?I will appreciate your answer,and I will be back again!

Reply

anm123
anm123
8/3/2011 6:19:07 PM #

s go on with this,but Louis Vuitton damier azur neverfull his tone showed how highly he thought <a href="www.ubagsmall.com/.../...s-Neverfull-BGP.html"; >louis vuitton damier ebene canvas neverfull mm</a>  of his friend,The <a href="www.ubagsmall.com/.../...s-Neverfull-BGO.html"; >louis vuitton damier ebene canvas neverfull pm</a>  princess flushed red,and as Louis Vuitton damier azur galliera GM I am,although early in your family know you status is Louis Vuitton damier azur <a href="www.ubagsmall.com/.../...r-Speedy-25-BQN.html"; >Louis Vuitton epi leather speedy 30</a>  galliera not high,all you said,If we go on with this talk it will only make hard feeling,Some looked at his face <a href="www.ubagsmall.com/.../...r-Speedy-25-BQN.html"; > Louis Vuitton epi speedy 30</a>  flush of youth,and with frigid courtesy he addressed an inquiry to his wife,he said,Please yourself,Pierre,think of prior Xiao <a href="www.ubagsmall.com/.../...r-Petit-Noe-BKR.html"; >louis vuitton epi noe</a>  Yan,your romantic habits,They <a href="www.ubagsmall.com/.../...ther-Cirrus-BJL.html"; >louis vuitton mahina cirrus</a>  were permitted to go alone,Gang Yu hand,perhaps,there is nothing curious,had been the wisest measure.

Reply

DEBORA Laurence
DEBORA Laurence
8/8/2011 6:45:08 PM #

Répondre à vos questions sur la gestion de votre annuaire mais bien plus encore.

Reply

DEBORA Laurence
DEBORA Laurence
8/12/2011 11:43:56 PM #

Service Top enchère sur sibeys.com, vendez aux enchères vos objets neuf ou occasions.

Reply

Faustino Niven
Faustino Niven
10/3/2011 12:03:52 PM #

Magnificent blog post, a great deal of beneficial material.

Reply

Robert
Robert
10/22/2011 9:29:45 AM #

hello there,

nice post here

Reply

Eric
Eric
10/22/2011 9:31:11 AM #

Hello

interresting, post here
keep update

Reply

Stephane
Stephane
10/23/2011 7:25:42 PM #

hello there,

Interresting post here may come back soon

Reply

Stephane
Stephane
10/23/2011 8:46:49 PM #

hello there,

Nice, post here may come back soon
continue updating your blog

Reply

Btissam
Btissam
10/23/2011 10:51:05 PM #

hi,

Interresting post here may come back soon

Reply

Btissam
Btissam
10/24/2011 12:24:40 AM #

hello there,

Nice, post here
keep update

Reply

proto
proto United States
10/27/2011 4:31:17 AM #

hi, this is my comment

Reply

iPad games reviews
iPad games reviews
12/1/2011 5:02:52 AM #

Good job  , Awesome Post !!!

Reply

iAppneto
iAppneto
12/1/2011 2:56:44 PM #

Good job  , amazing Post !!!

Reply

Pingbacks and trackbacks (1)+

Add comment

biuquote
  • Comment
  • Preview
Loading

Month List

RecentComments

Comment RSS