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
     
         
  • 相关阅读:
    Path Sum
    【转】Python的神奇方法指南
    【转】浅析Python中的struct模块
    【转】Python之系统交互(subprocess)
    【转】Win10下python3和python2多版本同时安装并解决pip共存问题
    【转】python包导入细节
    【转】python 历险记(四)— python 中常用的 json 操作
    【转】python模块导入细节
    【转】python 内置函数总结(大部分)
    【转】MySQL-Select语句高级应用
  • 原文地址:https://www.cnblogs.com/wintersun/p/1561161.html
Copyright © 2011-2022 走看看