【问题标题】:Website down for maintenance page on IIS for external visitors网站关闭以在 IIS 上为外部访问者提供维护页面
【发布时间】:2015-02-27 00:22:09
【问题描述】:

当我在 IIS 中托管的网站上进行一些主要部署时,我想建立一个网站以供“外部”访问者维护。我已阅读有关在 Web 根目录中创建“App_Offline.htm”页面的信息。我是否仍然可以在 localhost 下浏览网站(我将登录服务器),而通过域名浏览的访问者会看到“停机维护”页面?在将更改提供给访问者之前,我想进行一些测试。

【问题讨论】:

  • 我可以建议您在serverfault.com 提出这个问题吗? Stack Overflow 用于编程问题。这更适用于系统管理。
  • 我投票 +1 作为一个想法,因为你只能通过编程来解决它。

标签: asp.net iis


【解决方案1】:

默认情况下,当您打开app_offline.htm 时,asp.net 会停止该网页的运行。

但是您可以通过 global.asax 上的一些编程来做到这一点:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    HttpApplication app = (HttpApplication)sender;

    // if its not local - let see if we go offline
    if (!app.Request.IsLocal)
    {
        // the file we look now is the app_offline_alt.htm
        string cOffLineFile = HttpRuntime.AppDomainAppPath + "app_offline_alt.htm";

        // if exist on root
        if (System.IO.File.Exists(cOffLineFile))
        {
            using (var fp = System.IO.File.OpenText(cOffLineFile))
            {
                // read it and send it to the browser
                app.Response.Write(fp.ReadToEnd());
                fp.Close();
            }

            // and stop the rest of processing
            app.Response.End();
            return;
        }
    }
}

【讨论】:

  • 好答案。我们必须在流中读取文件吗?或者我们可以简单地对离线 html 文件执行 response.redirect() 或 server.transfer() ?是否有使用 URL Rewrite 的解决方案?
  • @TejSoft 首先,如果您投票并接受,我将不胜感激。其次,除了直接写入之外的任何事情都会导致问题,server.transfer()/redirect 可能会在此时崩溃,因为您创建了一个无限循环。
  • 这个解决方案是迄今为止最好的。
【解决方案2】:

我通过以下方式解决了这个问题:

  1. 我在 web.config 文件中添加了一个名为“MaintanenceMode”的键

<appSettings> <add key="MaintenanceMode" value="true" /> </appSettings>

  1. 我在主控制器中添加了一个动作结果和视图

public class HomeController : BaseController { public ActionResult Offline() { return View("Offline"); } }

  1. 我更改了 Global.asax.cs 文件检查的 MaintenanceMode 键,并将所有外部请求(图像、css 和 js 文件请求除外)重定向到离线页面

protected void Application_BeginRequest() { if (WebConfigurationManager.AppSettings["MaintenanceMode"] == "true") { if (!Request.IsLocal && !Request.Path.Contains("bundles") && !Request.Path.Contains("Content")) { HttpContext.Current.RewritePath("/home/offline"); } } }

【讨论】:

    猜你喜欢
    • 2011-02-08
    • 2011-05-31
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2012-11-25
    • 2021-10-03
    相关资源
    最近更新 更多