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:
- Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components
- How To Create an ASP.NET HTTP Module Using Visual C# .NET
Regards,
Anas Ghanem