How to Make an Application Offline in ASP.NET 2.0
 
Published: 27 Jun 2008
Abstract
There are different approaches to taking an application offline, for such things as say maintenance purposes. The latest addition is App_Offline.htm in ASP.NET 2.0 and this article demonstrates how to perform this task. First, is a brief overview of IIS and then the usage of httpRuntime and App_Offline.htm are provided along with detailed analysis and screenshots.
by SANJIT SIL
Feedback
Average Rating: 
Views (Total / Last 10 Days): 27646/ 33

Introduction

In the real world, there may be downtime for a web site even after web application has been deployed and is live on the Internet or intranet. This is because most applications require some updating with the flow of time. For example, there may be the possibility of error in an application in the form of a bug; there may be an enhancement for new features or modification of existing features, etc. After fixing those bugs and implementing the enhancements, the updated code needs to be deployed to the production environment. This may involve updating pages, configuration files, making database modifications, image update, DLL update and so on.

When updating a web application that is currently in production, it is best to take the application offline so that users can understand that there is some work going on for the application. This can be done in a variety of ways, from simply stopping the web server software to displaying a web page that informs the user that the site is offline for maintenance. The bottom line is not to show customer/user any error page, instead inform them with a proper message so that they will not be frustrated even though the application is offline. The sample code snippets have been written in C#.

Using IIS

Using Microsoft's Internet Information Services (IIS) web server software, we can make an application offline. The approach is to instruct IIS to route all incoming requests (for ASP.NET pages, HTML pages, images, and so on) for the web site to another URL automatically. For example, assume we have an ASP.NET application hosted on a server; we can setup IIS so that all requests to that application are rerouted to a different site or URL.

To configure an application to redirect all requests to another URL, we have to do the following.

From IIS manager -> right-click on the application -> choose Properties. From the Virtual Directory tab, indicate that the content for this application should come from "A redirection to a URL." Then enter the URL to redirect to and check "The exact URL entered above" checkbox, as shown in the screenshot below. It should be noted that in shared, hosted web environments we may not have the ability to edit IIS settings.

Figure 1

Using <httpRuntime>

The <httpRuntime> section configures some run time parameters for the ASP.NET pipeline. One attribute in the <httpRuntime> element is enabled, which indicates whether the AppDomain is enabled to accept an incoming request. The default value is "true," but we can override the default value in Web.config. By setting this value to "false," the application is not loaded in memory and any request entering the ASP.NET engine for the application will return the default ASP.NET 404 error status page. It should be noted that any requests not handled by the ASP.NET engine (images files, CSS files, static HTML pages, and so on) will return the normal way. Only ASP.NET resources will report a 404 status. To illustrate the same we can set the value false for <httpRuntime> in order to make the application offline. The following code snippet in applicaiton's Web.config is showing the same.

Listing 1

<configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
      <httpRuntime enable="false" />
    </system.web>
</configuration>

Once the abovementioned change has been made, users will get the standard ASP.NET 404 error message for requesting any ASP.NET resource.

Figure 2

Using App_Offline.htm

Disabling the <httpRuntime> approach provides no feedback to the end user. Fortunately, there is a cool feature available in ASP.NET 2.0 for taking the application offline - the App_Offline.htm file. If we place a file called App_Offline.htm in the root of the website, all requests to this site will now return the content of this html-file. We can place any static content in this file. It will cause the application domain to recycle. The same will happen when we remove the file. The application domain will also recycle if we edit the file, in the same way that an application will recycle when we edit web.config

The main advantage of App_Offline.htm over <httpRuntime> is that with App_Offline.htm, the content of the App_Offline.htm file is returned to the client. So we can place a rich App_Offline.htm file with a combination of both text and graphics explaining why the website is unavailable. There are some points that we should keep in mind. First, the ASP.NET runtime will not return an App_Offline.htm file if it exceeds 1 MB in size. Moreover, if our App_Offline.htm file is too small (less than 512 bytes), Internet Explorer will display its "Friendly Http Errors" (this can be configured in the Tools->Internet Options->Advanced tab within IE, and is on by default with IE6) rather than the content returned by App_Offline. So developers should ensure the App_Offline.htm is at least 512 bytes long.

That means that users are basically locked out of the application and we are free to make any changes.

Figure 3

If we look at the address bar in the abovementioned screen shot, we will see that we did not actually request app_offline.htm. We requested the application's WebForm1.aspx page and were sent the contents of the app_offline.htm file instead.

The usage scenario for this is for site maintenance and it can be easily included as part of a deployment script, where this is the first file added and the last file deleted during the deployment.

It should be noted that we should remove the App_offline.htm file after we are finished copying the files. Once removed, the next incoming request will bring the ASP.NET 2.0 web application back online.

Lastly, we can write our own custom code in order to show the user a proper message instead of an error page. Readers can see my articles named “Working with Custom Error Pages Using ASP.NET.”

Conclusion

Just taking an application offline does not necessarily mean that the site is down. But in order to inform the user, a proper method selection is required when we take our application offline. Out of the different techniques mentioned above for taking a web application offline, the most reliable  way is from IIS, since that guarantees that all requests will be either rejected or rerouted to some predefined page. But the selection surely will depend on how and where our website is hosted; we may not have access to the IIS manager. In such cases, there are a variety of ways to take an application offline through ASP.NET techniques. It can be <httpRuntime> settings, App_Offline.htm or custom code. The newly added App_Offline.htm feature in ASP.NET 2.0 is really a nice way for implementing an Under Construction or Down for Maintenance message to all users while we are upgrading features, fixing bugs, or making web design changes in a Website.

References

App_Offline.htm

 



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-19 3:12:38 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search