I often map HTML extension to the ASP.NET DLL in order to use URL rewriter with .html extensions. In a recent project, we renamed all URLs to end with .html. This works great, but failed when we used FCK Editor. Static HTML files would not get served because we mapped the HTML extension to the .NET Framework. We can use .html extension with our rewriter but still want to use IIS behavior with static HTML files.
I thought that this could be resolved with a simple HTTP handler. We would map URLs of static files in our rewriter to this handler that would read the static file and serve it, just as IIS would do.
This is how I coded the class. Note that this may not be bullet proof. I only tested it once and I am sure that the logic behind IIS is more complicated that this. If you find errors or think of possible improvements, let me know.
Public Class ISAPIDotNetHandler
Sub ProcessRequest(ByVal context As HttpContext) _
context.Response.ContentType = "text/html"
Dim uri As String = context.Request("fileUri")
If String.IsNullOrEmpty(uri) Then
Throw New ApplicationException("No fileUri")
If Not uri.ToLower.EndsWith(".html") Then
Throw New ApplicationException("Extension not allowed")
Dim fullPath As String = context.Server.MapPath(uri)
Dim stream As IO.StreamReader = _
stream = Nothing
Catch ex As Exception
context.Response.StatusCode = 404
ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
As you see, with our static files mapping to this handler using query string (e.g.: /ISAPIDotNetHandler.ashx?fileUri=index.html) you will have the same behavior as if you ask for the URL/index.html.
Finally, test this only in IIS with the HTML extension map to aspnet_isapi.dll. URL rewriting will work in Casini (Internal Web Server shipped with Visual Studio) but it's not the same as with IIS since EVERY request is handled by .NET.
Published: 4/19/2009 11:25 PM