Click here to Skip to main content
15,886,724 members
Articles / Web Development / ASP.NET
Tip/Trick

Put the website in Maintenance Mode (Under Construction)

Rate me:
Please Sign up or sign in to vote.
4.88/5 (25 votes)
25 Dec 2011CPOL 94.7K   32   16
This Tip/Trick shows how to make the website available only for the local clients
First, you need to add a key/value to your web.config file, so you can turn the maintenance mode ON or OFF.

C++
<add key="MaintenanceMode" value="false"/> <!-- true/false -->


Then put this code block in the Application_BeginRequest of the Global.asax

C#
void Application_BeginRequest(object sender, EventArgs e)
      {
          if (ConfigurationManager.AppSettings["MaintenanceMode"] == "true")
          {
              if (!Request.IsLocal)
              {
                  HttpContext.Current.RewritePath("maintenance.aspx");
              }
          }
      }

Now if you set the "MaintenanceMode" key in the config to true, all the requests from the remote clients will be redirected to the maintenance.aspx page, but the requests from local client will be served normally.

Also if you want to make the application available for certain IPs (to test the application from different machines or restrict the usage to them only), you may include their IPs in the web config and add this to your code:

if (!Request.IsLocal && !allowedIPs.Contains(Request.UserHostAddress))

License

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


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
I hold a BS degree in software engineering and am a Microsoft Certified Solution Developer(MCSD).
I have more than 8 years of experience in .NET developement, mostly web develop using C# and ASP.NET.

Comments and Discussions

 
GeneralReason for my vote of 5 Thanks for this nice Configuration. Pin
Member 43208443-Jan-12 7:39
Member 43208443-Jan-12 7:39 

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.