I am trying to add custom error pages to my web application. So far I have added this to my web.config file under the element:
<customErrors mode="On" >
<error statusCode="404" redirect="~/404.aspx"/>
<error statusCode="500" redirect="~/500.aspx"/>
</customErrors>
This works fine for errors that .NET touches for example a url that contains the .aspx extension. However I also want custom errors to display for a url such as www.example.com/dasda
Currently when I request a page such as the above IIS 7.5 displays it's own error message. I have added this under the element:
<httpErrors >
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="~/404.aspx" responseMode="ExecuteURL" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="500" path="~/500.aspx" responseMode="ExecuteURL" />
</httpErrors>
I thought that this would make IIS display a custom error page instead of it's default ones but this doesn't seem to be the case.
I am aware that I can set a custom error page in IIS itself but an ideal solution for my situation would be to have this configurable in the web.config.
I have tried adding this into my custom error pages on the Page_Load event as suggested here :
Response.TrySkipIisCustomErrors = true;
However it did not stop the default IIS page from showing in place of my custom error page. I have also tried what is suggested here:
<httpErrors >
<remove statusCode="404" subStatusCode='-1' />
<error statusCode="404" path="~/404.aspx" prefixLanguageFilePath='' responseMode="Redirect" />
<remove statusCode="500" subStatusCode='-1' />
<error statusCode="500" path="~/500.aspx" prefixLanguageFilePath='' responseMode="Redirect" />
</httpErrors>
But this has also not worked.
So is there a way to prevent IIS from displaying default error pages by configuring settings in the web.config file?
The problem I was encountering was that by default has an attribute errorMode with the following options: DetailedLocalOnly, Custom or Detailed.
If the errorMode attribute is left unspecified as I was doing then it defaults to DetailedLocalOnly (ref). Which means I would not have seen the custom error that was displayed.
The configuration settings that worked were:
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode='-1' />
<remove statusCode="500" subStatusCode='-1' />
<error statusCode="404" path="/404.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL" />
<error statusCode="500" path="/500.aspx" prefixLanguageFilePath="" responseMode="ExecuteURL" />
</httpErrors>
The errorMode="Custom" is handy to test the custom pages are working correctly but is probably more handy when left omitted or set explicitly to errorMode="DetailedLocalOnly" for debugging purposes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With