简体   繁体   English

如何处理404错误

[英]How to handle 404 errors

How can I handle ALL 404 errors? 如何处理所有 404错误?

If I goto a page that does not exist 如果我转到一个不存在的页面

Example: http://www.example.com/Idontexist.aspx 示例: http//www.example.com/Idontexist.aspx

It works fine.. 它工作正常..

However, http://www.example.com/Nothere.nope or http://www.example.com/NotHere gives a 404.0 error, I think its due to .nope not registered in IIS, but would there be a way to handle this in the web.config or in code? 但是, http://www.example.com/Nothere.nopehttp://www.example.com/NotHere给出了404.0错误,我认为它是由于.nope没有在IIS中注册,但是会有办法在web.config或代码中处理这个?

This likely because notthere.nope isn't passing through .NET. 这可能是因为notthere.nope没有通过.NET。 Depending on the version and configuration of IIS, ASP.NET is only used to execute requests with specific extensions. 根据IIS的版本和配置,ASP.NET仅用于执行具有特定扩展的请求。

Ideally, you should trying handling 404's at the IIS level - there is no point in letting it bubble up that far the application stack. 理想情况下,您应该尝试在IIS级别处理404 - 没有必要让它在应用程序堆栈中冒出来。 This would be my recommendation. 这是我的建议。

If you really want ASP.NET to be able to handle this, you can If you are using IIS 7 there is a nice write-up on that here . 如果你真的想ASP.NET能够处理这个问题,你可以,如果你使用的是IIS 7上有一个很好的写了这里

If you are using IIS 6, you can use a wildcard mapping to the ISAPI extension as described here here . 如果使用的是IIS 6,您可以使用通配符映射为这里所描述的ISAPI扩展在这里

You can, but you have to do it at the server level. 您可以,但必须在服务器级别执行此操作。 Depending on your webserver and/or IIS version the exacts of how you do this can be quite different. 根据您的网络服务器和/或IIS版本的你如何做到这一点,需要付出可以说是相当不同的。 Unfortunately I cannot give you a: "Go do this" answer, and the chances are that you'll need to make modifications as time goes by and server platforms evolve. 不幸的是,我不能给你一个:“去做这个”答案,你可能需要随着时间的推移和服务器平台的发展进行修改。

both. 都。 easiest way would be to install http://www.iis.net/download/urlrewrite and the configure it to redirect. 最简单的方法是安装http://www.iis.net/download/urlrewrite并将其配置为重定向。 Assuming you want all kinds of "interesting" URLs, that would be easiest. 假设您想要各种“有趣”的URL,那将是最简单的。 The web.config portion would look like this: web.config部分看起来像这样:

    <system.webServer>
      <rewrite>
        <rules>
          <rule name="My404" stopProcessing="true">
            <match url="^/Nothere\.nope" />
            <action type="Rewrite" url="/NotHere" />
          </rule>
        </rules>
      </rewrite>
    </system.webServer>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM