9

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?

Community
  • 1
  • 1
Aesir
  • 2,033
  • 1
  • 28
  • 39

3 Answers3

15

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.

Aesir
  • 2,033
  • 1
  • 28
  • 39
0

I think you need to force IIS to use the CustomErrorModule for any resource requested by the user. Try to add the below to your web config

 <modules>
      <add name="CustomErrorModule" type="customErrorModule" preCondition="" />
 </modules>

Have also a look at the below link

https://serverfault.com/questions/53712/in-iis-7-how-do-i-set-up-a-default-error-document-for-any-error

Community
  • 1
  • 1
Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • 1
    Thanks, what part of the web.config should this be added to? I tried putting it in the httpModules and the modules section but neither worked. – Aesir Jul 13 '11 at 11:49
  • Thank you the update however it still didn't work. I have managed to get it working. – Aesir Jul 13 '11 at 14:10
0

In the case you have a JSON response coming from your server, by setting existingResponse="PassThrough", you instruct IIS to pass through the HTTP error response without handling it. This allows your server's JSON response to be sent to the client directly.

</system.webServer>   
    <!-- Avoid showing the default IIS error pages and display JSON response -->
    <httpErrors existingResponse="PassThrough" />
</system.webServer> 

testing_22
  • 2,340
  • 1
  • 12
  • 28