Taking ASP.NET 2.0 Application Offline

That's really one of the cool things about ASP.NET 2.0, taking your application offline, and basically you will want to do that at many situations, such as marinating or updating your application which may take your application down, and users will get inconsistent response, like Server Error exception page, so you should provide a consistent response while your application is not serving users, also you may need to take the application offline to any other purpose such as releasing some 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 one, especially when you host your application on a shared hosting provider, also one of the situations that I have experienced before, is that you may be hosting your application on a webfarm and you upload and test your updates on one machine, , so it's advisable to take the server you are updating offline, while keeping the other servers up and serving users' requests, then 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.
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 you will application will stop to serve any upcoming requests and it will show the contents of the special app_offline.htm file to users, noting that this will not redirect users to that file, however the user will get the URL he requested into the browser's address bar wile the content shown will be grabbed from app_offline.htm, so this will keep the user sure that he is on the right location.
However, you have to take care about one interesting issue that Scott Guthrie blogged 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, that may leave users unsure whether the application is responding or not, so 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, also if you're adding inline styles in your file this will fix this issue.
In this article we will just have a ready 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 way to do this just like writing the file via FTP, or using Windows Explorer, in case you have access to the server, but 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 programmatic because your application would be already dead at that time!, but also you can do that pragmatically using another live application that have access rights to the first application folder, in this article we'll rename an 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 htm file, and the name of that htm file is 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 codebehind file, the page markup is empty, and herein the C# code for SetOffline.aspx page:

private string offlineContentFilename, offlineContentFilePhysicalPath, app_offlineContentFilename;
protected void Page_Load(object sender, EventArgs e)
{
offlineContentFilename = System.Web.Configuration.WebConfigurationManager.AppSettings["OfflinePageContentPath"];
offlineContentFilePhysicalPath = Server.MapPath(offlineContentFilename);
app_offlineContentFilename = offlineContentFilePhysicalPath.Substring(0,offlineContentFilePhysicalPath.LastIndexOf("\\"))+ "\\" + "app_offline.htm";
// here you have to check whether the user is authorized //take the app offline.//if (this.User.Identity.IsAuthenticated &&//this.User.IsInRole("Administrator"))//{ TakeApplicationOffline(); //}}private void TakeApplicationOffline(){try
{
if (System.IO.File.Exists(offlineContentFilePhysicalPath)){
System.IO.File.Move(offlineContentFilePhysicalPath,
app_offlineContentFilename);
Response.Write("Application has been turned OFF successfully, all upcoming requests will direct users to offline content page.");
}
else
{
Response.Write(offlineContentFilePhysicalPath + " doesn't exist, please make sure the file exists and try again!");
}
}
catch (Exception fileException)
{
Response.Write(fileException.Message);
}
}


The code is self-descriptive, however 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, and set the value to the file that contains the offline content you want 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 you will get html content contained in app_offline.htm, and all upcoming requests will just get this content, after you finish your update just go and rename app_offline to the same file name contained in web.config, then your application will come back to life.

Conclusion

That's a simple way to manage app_offline new feature, and you could do that manually if you prefer, but could make things more manageable, also you can change TakeApplicationOffline method to copy the file contents instead of renaming the file, and then you can simply delete the app_offline.htm, this will give you the same result.

Links to this post:

Create a Link