views:

161

answers:

3

As most of you would know, if I drop a file named app_offline.htm in the root of an asp.net application, it takes the application offline as detailed here.

You would also know, that while this is great, IIS actually returns a 404 code when this is in process and Microsoft is not going to do anything about it as mentioned here.

Now, since Asp.Net in general is so extensible, I am thinking that shouldn't there be a way to over ride this status code to return a 503 instead? The problem is, I don't know where to start looking to make this change.

HELP!

+1  A: 

You can try turning it off in the web.config.

<httpRuntime enable = "False"/>
leppie
+1  A: 

You could probably do it by writing your own HTTP Handler (a .NET component that implements the System.Web.IHttpHandler interface).

There's a good primer article here: link text

jules
That won't work, right? Because this technique doesn't even let the App Domain load, so I am thinking that the HTTP Handler will never get called.
Vaibhav
+5  A: 

The handling of app_offline.htm is hardcoded in the ASP.NET pipeline, and can't be modified: see CheckApplicationEnabled() in HttpRuntime.cs, where it throws a very non-configurable 404 error if the application is deemed to be offline.

However, creating your own HTTP module to do something similar is of course trivial -- the OnBeginRequest handler could look as follows in this case (implementation for a HttpHandler shown, but in a HttpModule the idea is exactly the same):

Public Sub ProcessRequest(ByVal ctx As System.Web.HttpContext) Implements IHttpHandler.ProcessRequest
    If IO.File.Exists(ctx.Server.MapPath("/app_unavailable.htm")) Then
        ctx.Response.Status = "503 Unavailable (in Maintenance Mode)"
        ctx.Response.Write(String.Format("<html><h1>{0}</h1></html>", ctx.Response.Status))
        ctx.Response.End()
    End If
End Sub

This is just a starting point, of course: by making the returned HTML a bit friendlier, you can display a nice "we'll be right back" page to your users as well.

mdb
That, I guess, is what I was hoping is not the case. I think I read in a Scott Gu article that there are events exposed by the pipeline for something like this. Now, I wonder if it really is hard coded.
Vaibhav
Yeah, it's definitely hardcoded. I've updated my answer to include the function name and source file in the ASP.NET reference source that shows that quite clearly...
mdb