如何在asp.net中重定向维护站点

时间:2013-08-05 17:48:39

标签: c# asp.net ihttphandler

我们在.NET中有一个ASP.NET网站,它在生产中成功运行。我们经常在特定的停机时间内维护服务器。因此,我们需要一项功能,可以在停机时将所有请求重定向到维护网页下的网站。我已经使用Custom Handler完成了这项任务,但我的客户对该解决方案并不满意。请提出一些替代解决方案。

我的自定义处理程序代码如下

在web.config下添加

    <system.webServer>
        <modules>
          <add name="CustomModule" type="CustomModule"/>
       </modules>
  </system.webserver>

在Http Handler下添加

public class CustomModule : IHttpModule
    {
        // In the Init function, register for HttpApplication 
        // events by adding your handlers.
        public void Init(HttpApplication application)
        {

            application.EndRequest +=
                (new EventHandler(this.Application_EndRequest));
        }
}

重定向代码在这里

private void Application_EndRequest(Object source, EventArgs e)
        {

            if (fileExtension.Equals(".aspx") == true && filePath.Contains("Contact.aspx") == false)
            {
                context.Response.Redirect("Contact.aspx");
            }
        }

3 个答案:

答案 0 :(得分:8)

只需使用 app_offline.htm ,解释herehere

如果要将网站保持特定时间段(或其他外部事件等),则可以在服务器上运行(计划)脚本以创建或删除app_offline.htm文件需要时。

答案 1 :(得分:5)

App_Offline.html会这样做。

在这里阅读App_Offline.html

答案 2 :(得分:0)

如果无法将代码部署到生产服务器,那么使用计划任务批处理文件(或自定义程序)按计划部署和删除App_Offline.html文件呢?