How to handle 404 errors
How can I handle ALL 404 errors?
If I goto a page that does not exist
Example: http://www.example.com/Idontexist.aspx
It works fine..
However, http://www.example.com/Nothere.nope or http://开发者_如何学JAVAwww.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?
This likely because notthere.nope
isn't passing through .NET. Depending on the version and configuration of IIS, ASP.NET is only used to execute requests with specific extensions.
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. 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.
If you are using IIS 6, you can use a wildcard mapping to the ISAPI extension as described here here.
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. 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. Assuming you want all kinds of "interesting" URLs, that would be easiest. The web.config portion would look like this:
<system.webServer>
<rewrite>
<rules>
<rule name="My404" stopProcessing="true">
<match url="^/Nothere\.nope" />
<action type="Rewrite" url="/NotHere" />
</rule>
</rules>
</rewrite>
</system.webServer>
精彩评论