понедельник, 31 января 2011 г.

Stop or Offline ASP.NET web application


The reason why you want to stop ASP.NET application could be if you do big website upgrade, which could include updating of pages, database, dlls etc. This process can take some time. If visitor tries to see pages, could experience errors. Sounds better to place a message that site is down for maintenance and that will be live again at certain time.
One more reason could be to unlock resources for updating. Some resources are locked while application is running, like SQL Server Express .mdf file in App_Data folder or MS Access .mdb file. You can't in the same time upload new database file and have active connections.
There are three common ways to make ASP.NET application offline:
  1. Using IIS to stop website
  2. Use App_Offline.htm file
  3. Use HttpRuntime element in Web.config
Using IIS
Simple solution is to stop website from IIS management console. More visitor friendly is to redirect all requests to info page where you can explain why website is down and when will be active again. On IIS 6,
Description: Http redirection in IIS 6
On IIS7, Http redirection is not available by default. First, go to Control Panel -> Turn Windows Features On or Off -> Internet Information Services -> World Wide Web Services -> Common HTTP Features -> and select Http Redirection like in next image:
Description: Enable HTTP redirect on Windows 7
Now, when you start IIS and select website, you will see new "HTTP Redirect" icon in Features View. Use is almost the same like on IIS 6, you need to specify exact destination for all requests. In addition to that, IIS 7 gives an option to set status code that will be returned to browser.
Accessing to Control Panel and IIS is impossible if you are on shared hosting where developer usually has very limited rights. Fortunately, ASP.NET offers similar option using App_Offline.htm file.
Going offline with App_Offline.htm file
Very simple way to make your web application offline is by using App_Offline.htm file. Create new HTML page and name it "App_Offline.htm". Copy the page to root folder of website. Now, if you try to visit website in browser, server will return only App_Offline.htm. Web server will shut down the application and unload it
On this page, you can place some information to visitors about why website is down, and when they could expect to be online again. You can put practically anything in this file, but be careful about its size. If App_Offline.htm is smaller than 512 bytes Internet Explorer (occurst on IE 6) will show its own HTTP error message. To avoid this, be sure that App_Offline.htm has more than 512 bytes, even if you place some invisible HTML comments.
When website upgrading is finished, delete or rename App_Offline.htm from root folder. Web application will go online again after next request.
Keep in mind that solution with App_Offline.htm page works only for ASP.NET pages. Many web hosting providers today offer several technologies in same hosting package, so you can use ASP.NET, PHP, classic ASP etc., all mixed on single website. Notice that any PHP, ASP or static HTML pages will still work normally after App_Offline.htm is uploaded.
Using httpRuntime element in web.config
To make web application offline, you can use Web Site Administration Tool. If you can't use WSAT, edit web.config manually. Add inside tag, like this:
<configuration>    
<system.web>
    <httpRuntime enable="false"/>
...
Application will not be loaded to memory. Any request to ASP.NET page will return error 404 "The resource cannot be found". This error is not friendly as App_Offline.htm file where you can inform visitors what is happening.
<httpRuntime enable="false"/> affects only ASP.NET pages, so content, like images, static HTML pages, classic ASP pages etc., will still be available.

Комментариев нет: