Change file root for StaticFileHandler

527 views Asked by At

I have an ASP.NET web project that (among other things) is serving some static files. By default, the framework assumes that all file paths are relative to the location of the web project. However, I'd like the actual root to be loaded from a custom setting in Web.config (e.g. "C:\MyStaticFiles\").

Is there any way to change what StaticFileHandler considers to be the server root directory? If not, is there an easy way to implement IHttpHandler that will make this change and hand off the rest of the work to the regular StaticFileHandler?

2

There are 2 answers

0
Ilya Builuk On

I think, from performance reasons, it isn't good idea to create different path mapping for static resources. They can be served by IIS directly without any ASP.NET processing if the are located in web site folder.

2
Lilith River On

For performance reasons, it's best to use URL rewriting rather than mess with an HttpHandler. StaticFileHandler is not as fast as IIS. Your own handler would be 10x slower than StaticFileHandler, and unless you are a really, really good engineer it would probably leak (or incorrectly hog) memory.

You can call context.RewritePath in the BeginRequest event (or PostAuthorizeEvent, if you use URL authorization) of your HttpModule or HttpApplication to do rewriting on select file types.