Putting your website in a maintanance mode based on a weekly schedule

In this blog i will talk about how to redirect the website visitors to a page that display a " web site  under maintenance"  ,

you can implement this functionality in your Global application file ( Global.asax file) ,

you can use  Application_BeginRequest event handler which will be called when your website receives a new request ,

this example will put your website in a maintenance mode if the current day is Saturday and for a one hour (from 10 -11)

add the following function to Global.asax file ,

(if you don't have this file in your website , in visual studio , right click  on the website , select add new Item , select global Application Class)

 

    Private Sub Application_BeginRequest(ByVal source As Object, ByVal e As EventArgs)

        Dim application As HttpApplication = CType(source, _
            HttpApplication)
        Dim context As HttpContext = application.Context

        If Now.DayOfWeek = DayOfWeek.Saturday AndAlso Now.Hour = 10 Then
            context.RewritePath("~/UnderMaintanace.aspx")
        End If
    
    End Sub


 adding  the above code will redirect your website users to UnderMaintanace.aspx page if the day is Saturday and if the time between 10 -11 .

  •  Note that i used RewritePath and not Response.redirect because the second one will make a new request to the website and so we will have infinite loop (until we redirect to an HTML page which will not handled by asp net runtime and the function will not get executed ) 
  • the above solution use the date and time of the week to put the website in a maintenance mode , but you can extend this to more practical scenarios ,

for example , you can use a settings file for your website, so that the administrator can set some flag and put the website in maintenance mode ! 

  • Another thing you may want to do is to use http module instead of using the Global application file , so that you can have a reusable module that can be plugged  to any new website you design.

Further resources: 

Regards,

Anas Ghanem 

 

Published Sunday, March 30, 2008 8:55 PM by anas
Filed under: ,

Comments

# re: Putting your website in a maintanance mode based on a weekly schedule

Sunday, March 30, 2008 9:58 PM by Jeremy

Or create an app_offline.html file since that is what it is for.

# re: Putting your website in a maintanance mode based on a weekly schedule

Sunday, March 30, 2008 10:09 PM by anas

Jeremy ,

app_offline.html is not always a solution , sometimes you need to show a custom message or give the users a choices to follow another links .....

Thanks

# re: Putting your website in a maintanance mode based on a weekly schedule

Sunday, March 30, 2008 11:52 PM by Hosam Kamel

Hello Anas,

you of course can do so using App_Offline.htm page

please check the following post at ScottGu'Blog

App_Offline.htm and working around the "IE Friendly Errors" feature

weblogs.asp.net/.../442332.aspx

# re: Putting your website in a maintanance mode based on a weekly schedule

Monday, March 31, 2008 1:57 AM by ryangaraygay

One advantage I could think of for this versus App_Offline.htm is that this is easier to automate. That is you can set custom date/time to display the maintenance page. Whereas to automate adding/deleting/renaming app_offline.htm is likely an overkill.

But then again this only applies if you just want to display the maintenance page and you won't be doing anything. Otherwise if you'd need to access the site and it's files anyway, it might be easier to just drop the app_offline.htm when you actually need to perform maintenance.

Nevertheless, thanks for the refresh on possible approaches to achieve this anas

# re: Putting your website in a maintanance mode based on a weekly schedule

Monday, March 31, 2008 1:47 PM by Ryan C Smith

This method could be convient when you want the application to be available to say a limited set of visitors (by IP address perhaps) yet in 'Maintenance Mode' to everyone else.

# re: Putting your website in a maintanance mode based on a weekly schedule

Monday, March 31, 2008 2:02 PM by anas

Hosam Kamel,

Yes , i read that post , however , what im talking about here is not just to show a friendly messages ( which may just works in IE) , I sohuld use a nother title to my post , any way , i don't think its a practical thing to go to every user browser and change some settins , if we did , we lose the meaning of " Web based applications "

Thanks

# re: Putting your website in a maintanance mode based on a weekly schedule

Monday, March 31, 2008 2:04 PM by anas

Ryan C Smith , Thanks for this ,

Yes there is many senarios that can be applied here , for example we can just allow the administrators to enter the website ! and we can prevent a users that comes from an  IP range ! there is many things we can provide here ...

Thnaks to all

# re: Putting your website in a maintanance mode based on a weekly schedule

Thursday, September 11, 2008 3:50 PM by Patrick

Anas,

Why do you use context.ReWritePath() and not Server.Transfer()?  I've done some looking around on the web and I can't see what the differences are between these two calls.

# re: Putting your website in a maintanance mode based on a weekly schedule

Thursday, October 07, 2010 12:21 PM by Mark Smith

I used your idea, but I have a question about images on my UnderMaintanace.aspx page.  It's not showing my images?

Thanks,

Mark

# re: Putting your website in a maintanance mode based on a weekly schedule

Wednesday, November 10, 2010 10:54 PM by Ross

That's because the request for the image is also getting trapped and processed, and thus returning the contents of the page instead of the image. This won't work with images as it is - you could check the file suffix of the context.Request.Path and pass through all except asp[x] & htm[l], or something like that.

# re: Putting your website in a maintanance mode based on a weekly schedule

Wednesday, December 08, 2010 8:49 AM by Nduh

how to set the end of maintanance mode

# re: Putting your website in a maintanance mode based on a weekly schedule

Tuesday, February 22, 2011 8:26 AM by shweta_chandaliya

I want to put my app in maintenance mode for visitors but my testers should be able to test the app. What do I do?

# re: Putting your website in a maintanance mode based on a weekly schedule

Wednesday, August 31, 2011 2:03 PM by Person1

If your testers have fixed IP connections, you could use Request.UserHostAddress to check whether to allow the requests through.  If not you will have to set a custom cookie which is checked instead.  Or if they are already logged in, check the user.

Leave a Comment

(required) 
(required) 
(optional)
(required)