Anas Ghanem

<ItemTemplate ".Net Tips" runat="Weblogs.asp.net" Country="WestBank" />

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 

 

Comments

Jeremy said:

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

# March 30, 2008 9:58 PM

anas said:

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

# March 30, 2008 10:09 PM

Hosam Kamel said:

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

# March 30, 2008 11:52 PM

ryangaraygay said:

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

# March 31, 2008 1:57 AM

Ryan C Smith said:

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.

# March 31, 2008 1:47 PM

anas said:

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

# March 31, 2008 2:02 PM

anas said:

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

# March 31, 2008 2:04 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)