Click here to Skip to main content
15,881,204 members
Articles / Web Development / ASP.NET

Taking an ASP.NET 2.0 Application Offline

Rate me:
Please Sign up or sign in to vote.
4.57/5 (11 votes)
17 Jul 2007CPOL5 min read 64.8K   265   50   15
Learn how to take your ASP.NET 2.0 application offline while updating or doing maintenance.

When your application is offline the users will get teh offline message, while the address bar will be pointing at the page user requested.

Introduction

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.

SetOffline.aspx

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:

C#
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; 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.

Conclusion

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Arab Emirates United Arab Emirates
Kareem Shaker is a .NET Architect , He's been working with VC++ and VB since version 4.0 ; Kareem has been working on design and development of many business applications , And he's now spending most of his time working with .NET Framework 1.x and 2.0 using both VB.NET and C# , Kareem has been giving some technical sessions targeted to .NET technologies, One of the technolgoies that I have passion to is EAI, I spend most of my times working on BizTalk Server 2006, I like to share knowledge and to interact with geeks around, Blogging is one of my hobbies, my blog is http://CairoCafe.Blogspot.com

Comments and Discussions

 
GeneralGood tip. And now how about... Pin
e2canoe24-Jul-07 3:36
e2canoe24-Jul-07 3:36 
GeneralRe: Good tip. And now how about... Pin
Kareem Shaker24-Jul-07 9:34
Kareem Shaker24-Jul-07 9:34 
GeneralHey Pin
rilov18-Jul-07 2:20
rilov18-Jul-07 2:20 
GeneralRe: Hey Pin
Kareem Shaker18-Jul-07 2:30
Kareem Shaker18-Jul-07 2:30 
GeneralThis is a nice one Pin
kmudda17-Jul-07 10:52
kmudda17-Jul-07 10:52 
GeneralRe: This is a nice one Pin
Kareem Shaker17-Jul-07 20:30
Kareem Shaker17-Jul-07 20:30 
GeneralGreat One Pin
APDevelop15-Feb-07 23:33
APDevelop15-Feb-07 23:33 
GeneralRe: Great One Pin
Kareem Shaker16-Feb-07 5:21
Kareem Shaker16-Feb-07 5:21 
NewsBe careful with access to SetOffline.aspx Pin
rlivelyppk25-Jan-07 10:07
rlivelyppk25-Jan-07 10:07 
GeneralRe: Be careful with access to SetOffline.aspx Pin
Kareem Shaker25-Jan-07 10:11
Kareem Shaker25-Jan-07 10:11 
AnswerRe: Be careful with access to SetOffline.aspx Pin
rlivelyppk25-Jan-07 10:31
rlivelyppk25-Jan-07 10:31 
GeneralRe: Be careful with access to SetOffline.aspx Pin
Vasudevan Deepak Kumar19-Jul-07 21:56
Vasudevan Deepak Kumar19-Jul-07 21:56 
GeneralThanks for the info Pin
Lee Forst24-Jan-07 3:09
Lee Forst24-Jan-07 3:09 
GeneralRe: Thanks for the info Pin
Kareem Shaker24-Jan-07 8:42
Kareem Shaker24-Jan-07 8:42 
Most welcome!

Kareem Shaker
http://cairocafe.blogspot.com

GeneralRe: Thanks for the info Pin
Vasudevan Deepak Kumar19-Jul-07 21:57
Vasudevan Deepak Kumar19-Jul-07 21:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.