AI OnAI Off
Hi,
To catch all errors, this is the simplest way (if you're using IIS7 and above):
<system.web>
<customErrors defaultRedirect="/Error.aspx" mode="On" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="/NotFound.aspx" />
<error statusCode="500" redirect="/Error.aspx" />
</customErrors>
</system.web>
...
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404" />
<error statusCode="404" path="/notfound.aspx" responseMode="ExecuteURL" />
<remove statusCode="500" />
<error statusCode="500" path="/error.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
And on your error pages you have to send the correct status codes
if (Request.HttpMethod == "GET")
{
Response.Status = "404 not found";
Response.StatusCode = 404;
}
And so on...
So in the customErrors section, do I have to define the error codes there as well as in the httpErrors, or can I get away with defining the error codes only in the httpErrors?
That's a good question. I guess you need all the 500 error codes in httpErrors at least, but maybe that's overkill. 500 is the most common.
The reason for customErrors is to catch other requests than .aspx, e.g. .htm and directories that doesn't exists.
And if you want the requested url on your 404 page:
string requestedUrl = Request.QueryString["aspxerrorpath"] ?? Request.RawUrl
I'm having some issues with the custom error page. I currently have the setup of the following:
Someone brought up the following point:
Hi!
Here’s an example:
When we try to retrieve a css file that was renamed, we don’t get a 404 anymore, we get the error page.
This means that it’s difficult to notice there was a 404 happening, since the browser doesn’t highlight the error.
Furthermore we get the cost of creating the complete error page when we aren’t accessing a page. We also fill up the log with different object access during the debug sessions with lookup to objects that we don’t expect at that time due to the error page being constructed.
Another example is if we have a publicly available url and we change that url. Then a crawler (say google) comes by and tries to fetch the old url. Then it won’t notice the 404 since it’s removed, but it will think that the url is correct and then it records it’s content, which is the error text. We really don’t want the error text indexed as a proper page in search engines.
I hope this clears out the issues we are having.
I’ve currently changed my web config with the setting existingResponse=”PassTrough”. That makes the css files 404 correctly, but I haven’t analyzed it further.
I thought this was the best way to setup the custom error page. Is there another way to do this because I haven't been able to find an alternate solution?