Click here to Skip to main content
Click here to Skip to main content

Put the website in Maintenance Mode (Under Construction)

By , 25 Dec 2011
 
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))

License

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

About the Author

Bahram Ettehadieh
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
Member
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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Question5 out of 5... Nice trickmemberrohit kakria14 Jun '12 - 23:16 
Its nice trick, keep it up
 
Rohit
xpode.com
QuestionHow to overide login.aspx?membertoy4fun14 May '12 - 20:45 
How to override the authentication when MaintenanceMode is true?
The site redirects to login.aspx and an Access is denied error is shown.
GeneralHttpContext.Current.RewritePath("maintenance.aspx"); gives f...memberMukund Thakker23 Feb '12 - 0:17 
HttpContext.Current.RewritePath("maintenance.aspx");
gives following error
The virtual path '/maintenance.aspx' maps to another application, which is not allowed.
GeneralIt might be better to redirect to a html page instead of .as...memberTomz_KV3 Jan '12 - 9:05 
It might be better to redirect to a html page instead of .aspx page during maintenance. app_offline.htm does not need any code. If you want to say something different to different users, you do need .aspx page.
GeneralReason for my vote of 5 Thanks for this nice Configuration.memberMember 43208443 Jan '12 - 7:39 
Reason for my vote of 5
Thanks for this nice Configuration.
GeneralReason for my vote of 5 its very simplemembermanojbdhobale28 Dec '11 - 19:05 
Reason for my vote of 5
its very simple
GeneralReason for my vote of 5 nice tipmemberMonjurul Habib28 Dec '11 - 8:17 
Reason for my vote of 5
nice tip
GeneralReason for my vote of 5 Simple to understand. Basic concept ...memberSunil Mane 00718 Dec '11 - 18:17 
Reason for my vote of 5
Simple to understand. Basic concept but effective
GeneralReason for my vote of 3 works, just not elegantmemberReiss29 Nov '11 - 4:49 
Reason for my vote of 3
works, just not elegant
GeneralReason for my vote of 5 Thanks for sharing.memberlinuxjr5 Jul '11 - 5:11 
Reason for my vote of 5
Thanks for sharing.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 25 Dec 2011
Article Copyright 2011 by Bahram Ettehadieh
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid