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 

 

10 Comments

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

  • 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

  • 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.

  • 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

  • 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.

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

    Thanks,
    Mark

  • 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.

  • how to set the end of maintanance mode

  • 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?

  • 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.

Comments have been disabled for this content.