65.9K
CodeProject is changing. Read more.
Home

Put the website in Maintenance Mode (Under Construction)

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.88/5 (24 votes)

Jul 4, 2011

CPOL
viewsIcon

98626

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.
<add key="MaintenanceMode" value="false"/> <!-- true/false -->
Then put this code block in the Application_BeginRequest of the Global.asax
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))