Here is the source code for another solution:
Global.asax:
void Application_BeginRequest(object sender, EventArgs e)
{
if (ConfigurationManager.AppSettings["MaintenanceMode"] == "true")
{
if ((ConfigurationManager.AppSettings["ByPassUrl"].Contains(
HttpContext.Current.Request.UserHostAddress.ToString())) || (Request.IsLocal))
{
}
else
{
HttpContext.Current.RewritePath("maintenance.aspx");
}
}
}
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:
Web.config:
<add key="MaintenanceMode" value="true" />
<add key="ByPassUrl" value="127.0.0.1" />
//Add list of IP addresses to bypass
from viewing offline page.