That's really one of the cool things about ASP.NET 2.0: that ability to take your application offline. You will want to do that at many situations such as maintaining or updating your application which may take your application down, and users will get inconsistent responses like Server Error exception page; you should provide a consistent response while your application is not serving users. Also, you may need to take the application offline for other purposes such as releasing resource locks on a textual file or Microsoft Access / SQL Express database file, and while you may use some other ways to do that, using this way is the easiest and best, especially when you are hosting your application on a shared hosting provider. One of the situations that I have experienced before is that you may be hosting your application on a webfarm and you want to upload and test your updates on only one machine, it's advisable to take the server you are updating offline while keeping the other servers up and serving user requests; once you finish updating and testing the first server, you can get it back online and then take all other servers offline, update them, then turn them back online.
app_offline.htm Special File
The idea to take your application offline is so simple, you will just need to write one file with a special name app_offline.htm into your main application directory. Afterwards, your will application will stop to serve any upcoming requests and it will show the contents of the special app_offline.htm file to users. Note that this will not redirect users to that file; however the user will get the URL he requested in the browser's address bar while the content shown will be grabbed from app_offline.htm, so this will ensure the user that he is at the right location.
However, you have to take care of one interesting issue that Scott Guthrie blogged about before: you have to make sure that your app_offline.htm content is greater than 512 bytes. Otherwise the content will not be shown and the generic friendly message of IE6 will be shown to users, which may leave users unsure of whether the application is responding or not. You can work around this by just adding comments into this file. You can read more about this workaround at Scott's blog, App_Offline.htm and working around the "IE Friendly Errors" feature. Adding inline styles to your file will also fix the issue.
In this article, we will just have a readymade HTML file that contains the offline content you want to show to your users while the application is offline, and we'll provide a humble programmatic approach to take the application offline, by simply renaming this file to the special app_offline.htm file name, and the file name that will be renamed will be saved in web.config. While you can think of other ways to do this just like writing the file via FTP, or using Windows Explorer, in case you have access to the server, I think that's the best way as you will just hit one page before doing your updates and then the application will be down; on the other hand, there is no way to take the application offline except deleting or renaming the app_offline file, and you will not be able to do that programmatically because your application would be already dead at that time! You can do that pragmatically using another live application that has access rights to the first application folder. In this article, we'll rename the existing file to app_offline.htm and then to get the application back to life, we will just rename that file to a predefined file name.
This webpage contains the code that will rename the HTML file, and the name of that HTML file will be contained in web.config. I added a server-side code block in the same SetOffline.aspx page to keep things compact; however, you can save the page logic into a code-behind file; the page markup is empty, and here is the C# code for the SetOffline.aspx page:
private string offlineContentFilename, offlineContentFilePhysicalPath,
protected void Page_Load(object sender, EventArgs e)
+ "\\" + "app_offline.htm";
private void TakeApplicationOffline()
Response.Write("Application has been turned OFF successfully," +
"all upcoming requests will direct users to offline content page.");
" doesn't exist, please make sure the file exists and try again!");
catch (Exception fileException)
The code is self-descriptive; it's just reading the offline content file name and composes the app_offline.htm file physical path, then renames the first file to app_offline.htm. Then you can simply take that page and copy it to your application and add a key to web.config with key value
OfflinePageContentPath. Set the value to the file that contains the offline content you want to show when the application is offline. Also, you should make sure that the user has enough rights to take the application offline.
Now, try to browse to any page under your application and you will get the HTML content contained in app_offline.htm, and all upcoming requests will just get this content. After you finish your update, just rename app_offline to the same file name contained in web.config, and your application will come back to life.
That's a simple way to manage the app_offline new feature, and you could do that manually if you prefer, but this method makes things more manageable. Also, you can change the
TakeApplicationOffline method to copy the file contents instead of renaming the file, and then you can simply delete app_offline.htm; this will give you the same results.