Take website offline for upgrade and display maintenance screen in ASP.NET 2.0 (and above)

Lately I’ve been doing a lot of research about Continuous Integration and Continuous Deployment practices, the latter being most relevant to this post. Basically the objective is to automate all manual labor of deploying code to production, with the obvious benefit of minimizing time and effort involved, but more importantly to reduce risk. As explained very eloquently in this book.

One important aspect of any deployment scenario is downtime. The first thing to consider about downtime is “opportunity”, that is the appropriate time for the upgrade process to take place. This is usually when we know site traffic is at it’s lowest so the impact is minimal. But even then we need to be prepared to greet users with a friendly message explaining the reasons for the downtime, especially to point out that it’s a programmed upgrade and not some catastrophic incident.

So this is where ScottGu has a great tip for us in this great post, which is really old (like 2006) but I honestly had never heard of this. Basically you place a file named “App_Offline.htm” (larger than 512 bytes, read the post) in the root folder an it will cause the application domain to shut-down, release all resources, and respond to all traffic with that static file. When you’re done just delete the file or rename it and it will magically bring the site up again. I prefer renaming the file because it’s always useful to have a quick way to bring down the site and display a maintenance screen.

This is really nice because the “.htm” file can contain any markup you wish to display to users during the downtime. One thing to notice here is that even though requests are being responded, for all intents and purposes the site is down. That means you can’t reference any css or images within the same site. So if you want to display an elaborate screen with images and styles you need reference them from another site.

Of course there are other more advanced alternatives to avoid the users from ever noticing downtime, but this works pretty well for more humble deployment strategies.