0

现在我可以在 web config 使用下面的代码生成自定义错误页面

<customErrors mode="On" defaultRedirect="Error404.aspx" redirectMode="ResponseRewrite">
  <error statusCode="404" redirect="~/Error404.aspx"/>
</customErrors>

这会捕获所有不存在的页面,只要它们不是 aspx

示例:http ://www.monstermmorpg.com/StackOverflow

现在,如果你这样做

http://www.monstermmorpg.com/StackOverflow.aspx

全局 asax 正在捕获错误

void Application_Error(object sender, EventArgs e)
{
    // Code that runs when an unhandled error occurs
    if (Server.GetLastError() != null)
        if (Server.GetLastError().GetBaseException() != null)
        {
            Exception objErr = Server.GetLastError().GetBaseException();
            ErrorLogger.LogError(Request.Url.ToString(), objErr.Message.ToString(), objErr.StackTrace.ToString());
            if (objErr.Message.IndexOf("does not exist") != -1)
            {

                // Response.Redirect("http://www.monstermmorpg.com");

            }
        }
}

但是在这里它告诉浏览器您要访问的页面存在。实际上它是不存在的。所以我想告诉浏览器你要找的页面是404。

我该怎么做=?

如果我在全局 asax 中重定向到 error404.aspx 页面,它会给我标头已发送的错误

这里错误

在此处输入图像描述

4

1 回答 1

0

再会

你会想尝试以下

从域到 www.domain 的全局 301 重定向

Response.Status = "301 Moved Permanently";   
Response.AddHeader("Location",   "http://"+host+port+"/404.aspx");
Response.End();
于 2013-11-04T11:56:59.147 回答