ASP.NET将页面重定向到新URL

时间:2012-04-30 17:14:17

标签: asp.net html redirect

我的网站有一个名为Test.htm的启动页面。该网站暂时关闭,我们希望在网站加载时显示错误页面。我有一个名为error.htm的页面。这怎么可能?

提前致谢!

5 个答案:

答案 0 :(得分:1)

ASP.NET提供了三种主要方法,允许您在错误发生时捕获并响应错误: Page_Error Application_Error 应用程序配置文件(Web.config)。

1. Page_Error事件处理程序提供了一种捕获页面级别发生的错误的方法 2.您可以使用Application_Error事件处理程序来捕获应用程序中发生的错误 3.如果未调用Server.ClearError或在Page_Error或Application_Error事件处理程序中捕获错误,则会根据Web.config文件部分中的设置处理错误。 在 部分中,您可以将重定向页面指定为默认错误页面(defaultRedirect),或者根据引发的HTTP错误代码指定特定页面。

e.g。您需要在Global.asax页面 customErrors 部分添加以下代码,以将用户重定向到自定义页面

<customErrors defaultRedirect="http://hostName/applicationName/errorStatus.htm" mode="On"> </customErrors>

答案 1 :(得分:0)

只是一个想法,但看了一个response.redirect?

In ASP.NET MVC, how does response.redirect work?

答案 2 :(得分:0)

答案 3 :(得分:0)

您可以破解web.config以强制您的应用程序在请求时返回404。然后将404错误页面覆盖为“错误”页面。

<httpRuntime enable="false" />

<customErrors mode="On" defaultRedirect="~/errors/GeneralError.aspx">
<error statusCode="404" redirect="~/error.htm" />
</customErrors> 

答案 4 :(得分:0)

您可以使用 app_offline.htm 页面。如果asp.net在root上找到这个页面,那么你要求它显示这个页面,并且网站已关闭。

第二种方式,即不将网站关闭,在应用程序开始请求上,重定向到您喜欢的页面:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
    string cTheFile = HttpContext.Current.Request.Path;

    // just double check in case that htm proceed from asp.net
    if(!cTheFile.EndsWith("test.htm"))
    {
         System.Web.HttpContext.Current.Response.Redirect("test.htm", true);
         return;
    }
}