Click here to Skip to main content
13,251,556 members (59,821 online)
Click here to Skip to main content
Add your own
alternative version


50 bookmarked
Posted 17 Jan 2007

Taking an ASP.NET 2.0 Application Offline

, 17 Jul 2007
Rate this:
Please Sign up or sign in to vote.
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.


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)

    offlineContentFilename = 

    offlineContentFilePhysicalPath = 

    app_offlineContentFilename = 
      + "\\" + "app_offline.htm";

    // here you have to check whether the user is authorized 
    //take the app offline.
    //if (this.User.Identity.IsAuthenticated &&

private void TakeApplicationOffline()
        if (System.IO.File.Exists(offlineContentFilePhysicalPath))
            Response.Write("Application has been turned OFF successfully," + 
                     "all upcoming requests will direct users to offline content page.");

            Response.Write(offlineContentFilePhysicalPath +
               " 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.


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


About the Author

Kareem Shaker
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

You may also be interested in...


Comments and Discussions

GeneralGood tip. And now how about... Pin
e2canoe24-Jul-07 4:36
membere2canoe24-Jul-07 4:36 
GeneralRe: Good tip. And now how about... Pin
Kareem Shaker24-Jul-07 10:34
memberKareem Shaker24-Jul-07 10:34 
GeneralHey Pin
rilov18-Jul-07 3:20
memberrilov18-Jul-07 3:20 
GeneralRe: Hey Pin
Kareem Shaker18-Jul-07 3:30
memberKareem Shaker18-Jul-07 3:30 
GeneralThis is a nice one Pin
kmudda17-Jul-07 11:52
memberkmudda17-Jul-07 11:52 
GeneralRe: This is a nice one Pin
Kareem Shaker17-Jul-07 21:30
memberKareem Shaker17-Jul-07 21:30 
GeneralGreat One Pin
Member #371453316-Feb-07 0:33
memberMember #371453316-Feb-07 0:33 
GeneralRe: Great One Pin
Kareem Shaker16-Feb-07 6:21
memberKareem Shaker16-Feb-07 6:21 
NewsBe careful with access to SetOffline.aspx Pin
rlivelyppk25-Jan-07 11:07
memberrlivelyppk25-Jan-07 11:07 
GeneralRe: Be careful with access to SetOffline.aspx Pin
Kareem Shaker25-Jan-07 11:11
memberKareem Shaker25-Jan-07 11:11 
AnswerRe: Be careful with access to SetOffline.aspx Pin
rlivelyppk25-Jan-07 11:31
memberrlivelyppk25-Jan-07 11:31 
GeneralRe: Be careful with access to SetOffline.aspx Pin
Vasudevan Deepak Kumar19-Jul-07 22:56
memberVasudevan Deepak Kumar19-Jul-07 22:56 
GeneralThanks for the info Pin
Lee Forst24-Jan-07 4:09
memberLee Forst24-Jan-07 4:09 
GeneralRe: Thanks for the info Pin
Kareem Shaker24-Jan-07 9:42
memberKareem Shaker24-Jan-07 9:42 
GeneralRe: Thanks for the info Pin
Vasudevan Deepak Kumar19-Jul-07 22:57
memberVasudevan Deepak Kumar19-Jul-07 22: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 18 Jul 2007
Article Copyright 2007 by Kareem Shaker
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid