Click here to Skip to main content
6,630,289 members and growing! (23,113 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Taking ASP.NET 2.0 Application Offline

By Kareem Shaker

Learn how to take your ASP.NET 2.0 application offline while you update or maintain it
C# 2.0, Windows, .NET 2.0, ASP.NET, WebForms, VS2005, Dev
Posted:17 Jan 2007
Updated:17 Jul 2007
Views:28,447
Bookmarked:45 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 3.79 Rating: 3.79 out of 5
2 votes, 20.0%
1
1 vote, 10.0%
2

3
2 votes, 20.0%
4
5 votes, 50.0%
5

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, taking your application offline, and basically 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 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.

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 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.

SetOffline.aspx

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Kareem Shaker


Member
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
Occupation: Architect
Location: United Arab Emirates United Arab Emirates

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralGood tip. And now how about... Pinmembere2canoe4:36 24 Jul '07  
GeneralRe: Good tip. And now how about... PinmemberKareem Shaker10:34 24 Jul '07  
GeneralHey Pinmemberrilov3:20 18 Jul '07  
GeneralRe: Hey PinmemberKareem Shaker3:30 18 Jul '07  
GeneralThis is a nice one Pinmemberkmudda11:52 17 Jul '07  
GeneralRe: This is a nice one PinmemberKareem Shaker21:30 17 Jul '07  
GeneralGreat One Pinmember0:33 16 Feb '07  
GeneralRe: Great One PinmemberKareem Shaker6:21 16 Feb '07  
NewsBe careful with access to SetOffline.aspx Pinmemberrlivelyppk11:07 25 Jan '07  
GeneralRe: Be careful with access to SetOffline.aspx PinmemberKareem Shaker11:11 25 Jan '07  
AnswerRe: Be careful with access to SetOffline.aspx Pinmemberrlivelyppk11:31 25 Jan '07  
GeneralRe: Be careful with access to SetOffline.aspx PinmemberVasudevan Deepak Kumar22:56 19 Jul '07  
GeneralThanks for the info PinmemberLee Forst4:09 24 Jan '07  
GeneralRe: Thanks for the info PinmemberKareem Shaker9:42 24 Jan '07  
GeneralRe: Thanks for the info PinmemberVasudevan Deepak Kumar22:57 19 Jul '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Jul 2007
Editor:
Copyright 2007 by Kareem Shaker
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project