0

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?

kpozin
  • 25,691
  • 19
  • 57
  • 76

2 Answers2

2

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.

Lilith River
  • 16,204
  • 2
  • 44
  • 76
  • How would I use URL rewriting to change the root folder from which files are served? This folder is outside of the web project folder. `context.RewritePath` still takes a virtual path -- it throws an error if you try to give it an absolute path. Is there something similar that would let me change the absolute physical path? – kpozin Nov 02 '11 at 14:55
  • No, you have to configure an IIS virtual folder to the physical path, then use RewritePath to point IIS there. Sorry I forgot to mention that. – Lilith River Nov 02 '11 at 15:10
0

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.

Ilya Builuk
  • 2,189
  • 2
  • 16
  • 10