Serving static content through the ASP.NET handler on Windows 2003 / IIS 6.0

I recently had the need to process static file types (particularly *.html and *.pdf files) through the .NET processor/Framework on a Windows 2003 / IIS 6.0 server for the purpose of redirecting certain URLs. Having done this in the past, I knew I simply needed to add an application mapping for each file extension to the .NET processor. (Read more about that at Microsoft’s page for How to: Configure an HTTP Handler Extension in IIS – in this case, for the last step, we want to make certain that “Verify that file exists” is NOT checked, because we want to redirect URLs for files that do not exist on the filesystem, therefore, you don’t want to check for the existence of those files.)

And it should Just Work™. (Provided you set up your redirect module properly, etc.)

 

IT. DOES. NOT. WORK.

 

Well, let me clarify: It worked in the case that I had a redirect setup for a file that did not exist on the filesystem. So, the actual redirect worked great. (yay!) However,

HOWEVER . . .

If there was a file on the filesystem with extension .html or .pdf that DID exist and I DID want to serve up to the client, it did NOT work. All I got was the error “The connection was reset”

Upon examination of the problem, IIS just drops/resets the connection without sending any content to the client web browser, which is what causes this error. (Now, this worked just fine in Windows 2000 / IIS 5, so I was perplexed at this behavior.)

 

On Win2K3, IIS 6, in the master web.config file (usually “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config“), under the section “<system.web><httpHandlers>” the next to last handler is “<add path="*" verb="GET,HEAD,POST" type="System.Web.DefaultHttpHandler" validate="true" />

That handler is what is supposed to serve up any content that is not handled by some other predefined .NET handler, such as the Page Builder handler for .aspx pages, for instance. IIS should use this “System.Web.DefaultHttpHander” to basically dump the content of the file to the IO Stream feeding your web browser’s connection to the server.

Unfortunately, this does not work properly on my default installs of Windows 2003 Web Edition.

 

To work around this problem, you need to redefine the http handler for your specific file types, or for the wildcard type. You can do this in a site specific web.config, or you can modify the system web.config to be what I consider to be “correct.”

In your site web.config, use the following:

  <system.web>
    <httpHandlers>
      <add path="*.html" verb="*" type="System.Web.StaticFileHandler" validate="true" />
      <add path="*.pdf" verb="*" type="System.Web.StaticFileHandler" validate="true" />
    </httpHandlers>
  </system.web>

Or to cover the wildcard extension (all files, with, or without, extensions):

  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.StaticFileHandler" validate="true" />
    </httpHandlers>
  </system.web>

And that will solve the problem for your site.

To solve it for your entire web server, though, AT YOUR OWN RISK (there may be unknown repercussions!!!), just change that “next to last” httpHandler in the system “C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\web.config” to use “System.Web.StaticFileHandler” instead of “System.Web.DefaultHttpHandler”.

Thanks very much to this resource at Microsoft.com: You receive a “Page cannot be displayed” error message when you use an ASP.NET application to open an .htm file or another static file that is hosted in IIS 6.0

2 thoughts on “Serving static content through the ASP.NET handler on Windows 2003 / IIS 6.0

  1. Pingback: [RESOLVED]How do I require a password for file download? | ASP Web Form Data Control

  2. Pingback: [RESOLVED]How do I require a password for file download? | ASP.NET MVC

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.