【问题标题】:Implement "Down for maintenance" page实施“停机维护”页面
【发布时间】:2011-11-26 17:04:14
【问题描述】:

我知道我们可以简单地使用 app_offline.htm 文件来执行此操作。

但是如果我的 IP 是 1.2.3.4(例如),我希望能够访问该网站,以便我可以进行最终测试。

if( IpAddress != "1.2.3.4" )
{
    return Redirect( offlinePageUrl );
}

我们如何在 ASP.NET MVC 3 中实现这一点?

【问题讨论】:

  • 您仍然可以仅使用 IIS 执行此操作。使用不重定向的不同主机名绑定设置新站点。无需用“部署”的东西污染你的代码。 :)
  • @bzlm 我们已经用不同的主机名完成了测试。我们需要使用真实主机名再次进行最终测试。
  • 我也看到了这种情况。
  • 我在 Azure IIS 上有一个 MVC 应用程序,并意识到用于 MVC 的 app_offline.htm 可能不是最佳选择。我很快使用了 app_offline.htm,但引用的样式表、图像和脚本文件可能会损坏。例如,IE Edge 在域末尾强制使用斜线 (/),而 google chrome 不包含斜线。我的参考资料坏了。对于 MVC,我认为路由需要负责,而不是 app_offline.htm

标签: asp.net-mvc asp.net-mvc-3 maintenance


【解决方案1】:

您可以使用带有 RouteConstraint 的包罗万象的路由进行 IP 检查:

确保你把离线路由放在第一位。

routes.MapRoute("Offline", "{controller}/{action}/{id}",
                new
                    {
                        action = "Offline",
                        controller = "Home",
                        id = UrlParameter.Optional
                    },
                new { constraint = new OfflineRouteConstraint() });

以及约束代码:

public class OfflineRouteConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        // return IpAddress != "1.2.3.4";
    }
}

【讨论】:

  • 非常干净的解决方案。正是我需要的。谢谢。
  • @Rickard,当我上传.DLL 以更新项目中的代码更改时,实时购物网站上的用户会出现错误,所以会停机寻求维护帮助吗?或statuspage.io 付费服务?这是关于类似issue的讨论
【解决方案2】:

您可以定义一个全局过滤器,如果它们不是来自您的 IP,则停止所有请求。您可以通过配置启用过滤器。

【讨论】:

    【解决方案3】:

    Per Max 的建议是一个实际的实现。

    public class MvcApplication : System.Web.HttpApplication
    {
    
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new CheckForDownPage());
    
        }
    
        //the rest of your global asax
        //....
    }
    public sealed class CheckForDownPage : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var path = System.Web.Hosting.HostingEnvironment.MapPath("~/Down.htm");
    
            if (System.IO.File.Exists(path) && IpAddress != "1.2.3.4")
            {
                filterContext.HttpContext.Response.Clear();
                filterContext.HttpContext.Response.Redirect("~/Down.htm");
                return;
            }
    
            base.OnActionExecuting(filterContext);
        }
    
    
    }
    

    【讨论】:

      【解决方案4】:

      我在 colemn615 的解决方案上遇到了无限循环,所以我添加了对离线页面的检查。

      此外,对于更高版本的 ASP.NET,它被拆分为 App_Start 文件夹中的 FilterConfig.cs 文件。

      public class FilterConfig
      {
      
          public static void RegisterGlobalFilters(GlobalFilterCollection filters)
          {
              filters.Add(new CheckForDownPage());
      
          }
      
          public sealed class CheckForDownPage : ActionFilterAttribute
          {
              public override void OnActionExecuting(ActionExecutingContext filterContext)
              {
                  if (HttpContext.Current.Request.RawUrl.Contains("Down.htm"))
                  {
                      return;
                  }
      
                  var path = System.Web.Hosting.HostingEnvironment.MapPath("~/Down.htm");
      
                  if (System.IO.File.Exists(path) && IpAddress != "1.2.3.4")
                  {
                      filterContext.HttpContext.Response.Clear();
                      filterContext.HttpContext.Response.Redirect("~/Down.htm");
                      return;
                  }
      
              base.OnActionExecuting(filterContext);
          }
      }
      

      【讨论】:

        【解决方案5】:

        我在 Web.Config 文件中添加了一个 AppSetting:

        <add key="MaintenanceMsg" value="We are currently performing some routine maintenance. We expect to be back up at around 01:00." />
        

        然后我更新 global.asax 文件的 Application_BeginRequest 方法以检查 appsetting 是否有值,如果有,我将所有内容重定向到维护页面:

        private void Application_BeginRequest(object sender, EventArgs e)
        {
                if(!string.IsNullOrWhiteSpace(ConfigurationManager.AppSettings["MaintenanceMsg"]) && !Request.Url.ToString().Contains("UndergoingMaintenance"))
                    Response.Redirect("~/Home/UndergoingMaintenance");
        
         ** Do whatever you do when you don't want to show your maintenance page here**
        }

        最后,创建视图和控制器动作。

        如果您使用 Azure,您只需在门户中添加和删除 AppSetting 的值,这样您就可以在几分钟内暂停您的站点而无需部署。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-02
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多