管理 ASP.NET 应用程序中的维护页面重定向
我想在 .config 文件中使用如下配置:
<appSettings>
<add key="SiteIsActive" value="false"/>
<add key="SiteNonActive_RedirectTo" value="UnderMaintainance.aspx"/>
</appSettings>
这样,当设置设置为 false 值时,系统会自动将每个请求重定向到维护页面。
我尝试以这种方式执行此操作:使用 Global.asax 的 Application_BeginRequest:
protected void Application_BeginRequest(object sender, EventArgs e) {
if ((bool)System.Configuration.ConfigurationManager.AppSettings["SiteIsActive"])
if (this.Request.Path.IndexOf(
System.Configuration.ConfigurationManager.AppSettings["SiteNonActive_RedirectTo"]) == -1)
this.Response.Redirect(
System.Configuration.ConfigurationManager.AppSettings["SiteNonActive_RedirectTo"]);
}
基本上它可以工作,但是当以这种方式重定向时,firefox 将向我显示没有应用任何图像或样式的页面...这很奇怪,我查看页面源代码通过浏览器下载,一切都在那里!
这是实现我的目标的正确方法吗? 我做错了什么吗?
谢谢
PS:Internet Explorer 的行为与 Firefox 不同,它正确地显示了重定向页面。
PS2:你们正确地向我发布了一个名为 App_Offline 的功能可用。好吧,我不想使用它,原因有一个:我想使用我的维护页面不仅显示一种状态,还显示更多状态,例如:
1) 维护
<appSettings>
<add key="SiteIsActive" value="false"/>
<add key="SiteNonActive_RedirectTo" value="UnderMaintainance.aspx?S=Maintainance"/>
</appSettings>
2) 正在建设
<appSettings>
<add key="SiteIsActive" value="false"/>
<add key="SiteNonActive_RedirectTo" value="UnderMaintainance.aspx?S=UnderConstr"/>
</appSettings>
3) 暂时不活动
<appSettings>
<add key="SiteIsActive" value="false"/>
<add key="SiteNonActive_RedirectTo" value="UnderMaintainance.aspx?S=TempInact"/>
</appSettings>
App_Offline 不提供给我这。
I would like to use a configuration in the .config file like this:
<appSettings>
<add key="SiteIsActive" value="false"/>
<add key="SiteNonActive_RedirectTo" value="UnderMaintainance.aspx"/>
</appSettings>
So that, when the setting is set to the false value, the system automatically redirects EVERY REQUEST to the maintainance page.
I tried to do this in this way: using Global.asax's Application_BeginRequest:
protected void Application_BeginRequest(object sender, EventArgs e) {
if ((bool)System.Configuration.ConfigurationManager.AppSettings["SiteIsActive"])
if (this.Request.Path.IndexOf(
System.Configuration.ConfigurationManager.AppSettings["SiteNonActive_RedirectTo"]) == -1)
this.Response.Redirect(
System.Configuration.ConfigurationManager.AppSettings["SiteNonActive_RedirectTo"]);
}
Basically it works, but when redirecting in this way, firefox will show me the page WITHOUT any image or style applied... it's strange, I look at the page source downloaded by the browser and everything is there!
Is this the right way to achieve my objective?
Do I do anything wrong?
Thankyou
PS: Internet Explorer does not behave like firefox, it shows me the redirected page correctly.
PS2: You guys correctly posted me that a feature called App_Offline is available. Well, I would like not using it for one reason: I would like to use my maintainance page not only to show one status, but more statuses, for example:
1) Maintainance
<appSettings>
<add key="SiteIsActive" value="false"/>
<add key="SiteNonActive_RedirectTo" value="UnderMaintainance.aspx?S=Maintainance"/>
</appSettings>
2) Under construction
<appSettings>
<add key="SiteIsActive" value="false"/>
<add key="SiteNonActive_RedirectTo" value="UnderMaintainance.aspx?S=UnderConstr"/>
</appSettings>
3) Temporary inactivity
<appSettings>
<add key="SiteIsActive" value="false"/>
<add key="SiteNonActive_RedirectTo" value="UnderMaintainance.aspx?S=TempInact"/>
</appSettings>
App_Offline does not offer me this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一种更简单的方法可以做到这一点,它使用一个名为
App_Offline.htm
的特殊页面。欲了解更多信息:
您可能还想要阅读 Scott 关于 IE6 问题的后续文章:
不使用时,只需将其重命名为
App_offline.disabled
,然后当您需要进行维护时,将其重命名为再次App_Offline.htm
。这种方法的另一个理由是,如果您的“维护”包括部署网站或编辑
web.config
,您的UnderMaintainance.aspx
页面可能无法执行,因为您的网站在上传时将处于不断变化的状态。或者,您可能犯了一个错误并覆盖了web.config
中的这些“维护”值。App_Offline.htm
很棒,因为它意味着您可能会真正搞乱您的部署,而没有人会知道。There's a much simpler way to do this and it uses a special page called
App_Offline.htm
.For more information:
You might also want to read the follow up by Scott about a gotcha with IE6:
When not in use just rename it to something like
App_offline.disabled
then when you need to do maintenance rename it back toApp_Offline.htm
again.A further justification for this approach is that if your "maintenance" includes deploying the site or editing your
web.config
, yourUnderMaintainance.aspx
page may fail to execute because your site will be in a state of flux whilst it is being uploaded. Or, you may have made a mistake and overwritten these "maintenance" values in yourweb.config
.App_Offline.htm
is great because it means you can really mess up your deployment and no-one will know.