错误404的默认重定向

She*_*Pro 27 asp.net redirect default http

我想在我的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不重定向

Max*_*oro 64

这是为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>
Run Code Online (Sandbox Code Playgroud)

正如其他人已经指出的那样,你不应该使用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>
Run Code Online (Sandbox Code Playgroud)

  • 是的,您的代码正在运行但是`<error statusCode ="404"path ="/ http404.aspx"responseMode ="ExecuteURL"/>'只显示我错误页面的空白页面.但是我用`<error statusCode ="404"path ="error.aspx"responseMode ="Redirect"/>`修复它.我知道我违反了你的3xx HTTp代码建议 (2认同)