IHttpModel

当我们在对某个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.youkuaiyun.com/vunal/archive/2009/09/08/4532427.aspx

转载于:https://www.cnblogs.com/longfeitengda/archive/2010/02/23/1671768.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值