Prevent IIS 7.5 from displaying default error pages using the web.config

Viewed 13831

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?

2 Answers
Related