Click here to Skip to main content
15,867,453 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 93.4K   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

 
Questionhow to add time for under maintenance for specific time Pin
Member 1473058429-Jan-20 23:28
Member 1473058429-Jan-20 23:28 
AnswerRe: how to add time for under maintenance for specific time Pin
Member 150394456-Jan-21 16:50
Member 150394456-Jan-21 16:50 
Question5 out of 5... Nice trick Pin
rohit kakria14-Jun-12 23:16
rohit kakria14-Jun-12 23:16 
QuestionHow to overide login.aspx? Pin
toy4fun14-May-12 20:45
toy4fun14-May-12 20:45 
GeneralHttpContext.Current.RewritePath("maintenance.aspx"); gives f... Pin
Mukund Thakker23-Feb-12 0:17
professionalMukund 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... Pin
Tomz_KV3-Jan-12 9:05
Tomz_KV3-Jan-12 9:05 
GeneralReason for my vote of 5 Thanks for this nice Configuration. Pin
Member 43208443-Jan-12 7:39
Member 43208443-Jan-12 7:39 
GeneralReason for my vote of 5 its very simple Pin
ManojDhobale28-Dec-11 19:05
ManojDhobale28-Dec-11 19:05 
GeneralReason for my vote of 5 nice tip Pin
Monjurul Habib28-Dec-11 8:17
professionalMonjurul Habib28-Dec-11 8:17 
GeneralReason for my vote of 5 Simple to understand. Basic concept ... Pin
WebMaster18-Dec-11 18:17
WebMaster18-Dec-11 18:17 
GeneralReason for my vote of 3 works, just not elegant Pin
Reiss29-Nov-11 4:49
professionalReiss29-Nov-11 4:49 
GeneralReason for my vote of 5 Thanks for sharing. Pin
linuxjr5-Jul-11 5:11
professionallinuxjr5-Jul-11 5:11 
GeneralReason for my vote of 5 Nice tips/Tricks.. Very simple in u... Pin
jawed.ace5-Jul-11 1:55
jawed.ace5-Jul-11 1:55 
GeneralThanks for the comment, fixed it Pin
Bahram Ettehadieh5-Jul-11 0:15
Bahram Ettehadieh5-Jul-11 0:15 
GeneralI think a typo my have crept into the spelling of maintenanc... Pin
George Swan4-Jul-11 19:54
mveGeorge Swan4-Jul-11 19:54 
GeneralReason for my vote of 5 Excellent tip; very simple and pract... Pin
DrABELL4-Jul-11 9:42
DrABELL4-Jul-11 9:42 

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.