zoukankan      html  css  js  c++  java
  • 使用HttpModules实现Asp.net离线应用程序

        当我们在对某个ASP.NET应用程序做维护时,此时可能还有某些客户会访问程序,此时如果你在更新某些程序,可能会出现那报错的页面或黄页。较好的做法是提供一个离线的页面,上面有些提示信息。例如,正在维护中,让客户过会儿在访问之类的。当客户访问default.aspx页面时,如果不是管理员那就让他们重定向到Offline页面。我们可以使用HttpModules来实现这功能,在服务器上放一个名为offline.html的文件,当时这个文件存在时,那么可以说维护模式开始了,然后在web.config中配置管理员机器的IP。

    <add key="AdminIP" value="127.0.0.1"/>
    HttpModules:
    /// <summary>
    /// This is how you take an ASP.NET application offline the 
    /// Arnold Matusz way with AppOfflineModule
    /// </summary>
    public class AppOfflineModule : IHttpModule
    {
        public void Dispose()
        {
    
        }
    
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(context_BeginRequest);
        }
    
        void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            HttpContext context = application.Context;
            if (File.Exists(Path.Combine(context.Server.MapPath("~"), "offline.html")))
            {
                string ip = context.Request.UserHostAddress;
                string adminIP = ConfigurationManager.AppSettings["AdminIP"];
    
                if (ip != adminIP)
                {
                    context.RewritePath("~/offline.html");
                }
            }
        }
    }
     

    在web.config中注册这个Module:

    <httpModules>
        <add name="AppOfflineModule" type="AppOfflineModule"/>
    </httpModules>
         好了,此时有这个Offline.html时,非管理中访问将显示:We are currently down for maintenance。
    这个简单的示例,可以扩展到权限的实现,更加丰富化UI交互。希望这篇Post对您有帮助!
         Author:Petter Liu   http://wintersun.cnblogs.com
     
         
  • 相关阅读:
    centos8 防火墙配置增加端口
    linux上搭建maven私服(下)
    linux上搭建maven私服(中)
    项目成本管理中的PV、EV与AC的区别-实例解释
    配置IKE SA的生存周期(华为)
    IKE SA和IPSec SA的区别
    IPsecVPN协商过程-主模式
    Fortigate防火墙常用命令
    飞塔防火墙清除系统密码
    fatal: unable to access ‘https://github xxxxxxxxx的解决方法
  • 原文地址:https://www.cnblogs.com/wintersun/p/1561161.html
Copyright © 2011-2022 走看看