我想在我的 ASP.net 网站中引入一个功能,每当收到对我域中未知 URL 的请求时,用户将被重定向到我在根目录中的 error_404.htm
页面应用程序。
例如,如果请求是http://www.mydomain.com/blahblahblah
然后我不想返回标准的 404 错误页面,而是希望它将请求重定向到 http://www.mydomain.com/error_404.htm
更新 IIS 版本 7.5 和 .NET Framework 版本 4
更新
/blah.aspx
重定向但 /blah
不重定向
最佳答案
这是为 ASP.NET 和非 ASP.NET 请求配置自定义 404 错误页面的方式:
<configuration>
<system.web>
<compilation targetFramework="4.0" />
<customErrors mode="On" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="http404.aspx" />
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/http404.aspx" responseMode="ExecuteURL"/>
</httpErrors>
</system.webServer>
</configuration>
正如其他人已经指出的那样,您不应该使用 HTTP 重定向将用户发送到主页,这不仅会让用户感到困惑,也会让机器(例如搜索引擎)感到困惑。使用 404 状态代码而不是 3xx 代码很重要。
您可以使用 HTML 上的元刷新实现所需的功能:
<%@ Page Language="C#" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Not Found</title>
<meta http-equiv="refresh" content="5;url=/"/>
</head>
<body>
<h1>Not Found</h1>
<p>Redirecting to Home...</p>
</body>
</html>
关于asp.net - 错误 404 的默认重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4483849/