IHttpModule

当我们在对某个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对您有帮助!

本文来自:http://blog.csdn.net/vunal/archive/2009/09/08/4532427.aspx
posted @ 2010-04-05 22:04  心随灵动  阅读(290)  评论(0编辑  收藏  举报