1

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

提前致谢!

4

5 回答 5

1

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

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

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

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

于 2012-04-30T17:23:42.880 回答
0

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

在 ASP.NET MVC 中,response.redirect 是如何工作的?

于 2012-04-30T17:18:34.990 回答
0

让它“离线”

请参阅:IIS:将所有请求重定向到一页?

于 2012-04-30T17:20:00.700 回答
0

您可以使用app_offline.htm页面。如果 asp.net 在 root 上找到这个页面,那么无论你问它什么都显示这个页面,并且该站点已关闭。

第二种方式,即不关闭站点,在 Application Begin Request 上,将重定向到您喜欢的页面:

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;
    }
}
于 2012-04-30T18:47:23.987 回答
0

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

<httpRuntime enable="false" />

<customErrors mode="On" defaultRedirect="~/errors/GeneralError.aspx">
<error statusCode="404" redirect="~/error.htm" />
</customErrors> 
于 2012-04-30T17:50:42.980 回答