Default redirect for Error 404

我们两清 提交于 2019-12-17 09:20:47

问题


I want to introduce a functionality in my ASP.net website that, whenever a request is received for an unknown URL on my domain, the user is redirected to my error_404.htm page in the root of the application.

For example, if the request is http://www.mydomain.com/blahblahblah

Then instead of returning the standard 404 error page, I want it to redirect the request to http://www.mydomain.com/error_404.htm

Update IIS Version 7.5 and .NET Framework Version 4

Update /blah.aspx redirects but /blah does not


回答1:


This is how you configure a custom 404 error page for both ASP.NET and non-ASP.NET requests:

<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>

As others already pointed out, you should not use an HTTP redirection to send the user to the home page, this is not only confusing to users but also to machines (e.g. search engines). It is important to use the 404 status code and not a 3xx code.

You can achieve the desired functionality using meta refresh on 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>



回答2:


You can point your users to a custom 404 by including the customErrors section in your web.config.

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

As others have stated in the comments above, sending users back to your homepage is going to be not only annoying but functionally confusing.

My advice would be to send the user to a page that gives them some information about what's happened and offers them a next step (browse other products, go back to your dashboard etc).




回答3:


In my case changing "ResponseRewrite" to "ResponseRedirect" worked:

My MVC Webconfig file Configuration:

   <system.web>
      <compilation targetFramework="4.0" />
      <customErrors mode="On" redirectMode="ResponseRedirect">
         <error statusCode="404" redirect="~/Home/Login" />
      </customErrors>
   </system.web>
</configuration>



回答4:


It's works for me. I just include this code between the tag <system.webServer> :

 <httpErrors errorMode="Custom">
      <remove statusCode="404"/>
      <error statusCode="404" path="~/Erro/PaginaNaoEncontrada.aspx" responseMode="Redirect"/>
    </httpErrors>


来源:https://stackoverflow.com/questions/4483849/default-redirect-for-error-404

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!