asp.nethttphandlerasp.net-routingiroutehandler

Only Serve Static Files From a Specific Directory via Code


I am looking for a code based solution to only serving static files from a specified directory using ASP.NET routing rather than specify a HttpHandler in the Web.config.

For example:

I have the following partial directory layout

/
/public
/public/hello.txt
/public/css
/public/css/base.css
/readme.txt

If any files are requested from the /public directory they should be served as-is. If any other static files are requested for example /readme.txt it should be served as a 404. The directory which allows static files to be served from should be easily specified as a string (var publicDir = "public";).

I am looking to do this with little to no configuration required in the Web.config file, and most of it configured in code.


Solution

  • I don't think it would be possible to achieve a solution using a class library. In IIS 6 environment files with .TXT or .JPG extension are served without going through .NET Framework. The only way is changing IIS configuration.

    In IIS 7 things are handled differently but I have no idea if it is possible to find a solution that works in all environments or not.

    I'll be pleased to hear what others say about this and correct me if i'm worng.