1

In Application the http status code errors are handled through web.config.

I have specified the http error code and respective path to show in case of http status error

<customErrors mode="On" redirectMode="ResponseRewrite"  />


 <httpErrors existingResponse="Replace" errorMode="Custom">
      <clear/>
      <remove statusCode="404"/>
      <error statusCode="404" path="Themes\Patient6\Static\404.html" responseMode="File"/>
      <remove statusCode="404" subStatusCode="980"/>
      <error statusCode="404" subStatusCode="980" path="/patient.search/search/noresults" responseMode="ExecuteURL"/>
      <remove statusCode="410"/>
      <error statusCode="410" path="Themes\Patient6\Static\410.html" responseMode="File"/>
      <remove statusCode="410" subStatusCode="990"/>
      <error statusCode="410" subStatusCode="990" path="/forums/discuss/deleted" responseMode="ExecuteURL"/>
      <remove statusCode="500"/>
      <error statusCode="500" path="Themes\Patient6\Static\500.html" responseMode="File"/>
      <remove statusCode="403"/>
      <error statusCode="403" path="Themes\Patient6\Static\403.html" responseMode="File"/>
    </httpErrors>

The above code is working fine for the given status code in httperrors but for the other error like 400, 502 the code is not handled so it shows the error explicitly.

Expected solution :

Is there any if-condition/Default-Path kind of solution to show custom error for given status code and 500 for other status code.

I tried the below workaround :

 <customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/error-500" />

But it redirect to 500 for all the error status code which gives false information for the user.

I also tried this :

<httpErrors existingResponse="Replace" errorMode="Custom" defaultResponseMode="File" defaultPath="Themes\Patient6\Static\500.html">

But getting the below error for the above line

HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.

Edit : enter image description here

devram
  • 105
  • 2
  • 11

1 Answers1

0

customErrors and httpErrors are two different things. See here for more information.

Your main issue here is that you're specifying error pages for ONLY those error codes, and you don't specify a default error page to use for any other error codes. As a result, it will show the error rather than a custom error page.

Googling how to do this quickly led me to this Stack Overflow page, which has the below code:

<httpErrors errorMode="Custom" existingResponse="Auto" defaultResponseMode="ExecuteURL" defaultPath="/App/Error"> <!-- Do not include ~, this was my issue all long -->
  <clear/> <!-- so that IIS provided error pages are skipped -->
  <!-- add those which you like to provide a view of yours -->
  <error path="/App/Http404" responseMode="ExecuteURL" statusCode="404"/>
  <error path="/App/Http503" responseMode="ExecuteURL" statusCode="503"/>
</httpErrors>

In terms of your code, this code below should work, assuming you have set up a default error page at Themes\Patient6\Static\error.html

<httpErrors errorMode="Custom" existingResponse="Auto" defaultResponseMode="File" defaultPath="Themes\Patient6\Static\error.html"> <!-- Do not include ~ -->
  <clear/> <!-- so that IIS provided error pages are skipped -->
  <error statusCode="404" path="Themes\Patient6\Static\404.html" responseMode="File"/>
  <error statusCode="404" subStatusCode="980" path="/patient.search/search/noresults" responseMode="ExecuteURL"/>
  <error statusCode="410" path="Themes\Patient6\Static\410.html" responseMode="File"/>
  <error statusCode="410" subStatusCode="990" path="/forums/discuss/deleted" responseMode="ExecuteURL"/>
  <error statusCode="500" path="Themes\Patient6\Static\500.html" responseMode="File"/>
  <error statusCode="403" path="Themes\Patient6\Static\403.html" responseMode="File"/>
</httpErrors>

You also shouldn't need to manually remove every statusCode, clearing once at the top should be sufficient.

jcreek
  • 43
  • 10
  • For anyone needing a tl;dr this was basically just missing the `defaultResponseMode="ExecuteURL" defaultPath="/App/Error"` from the `httpErrors` tag – jcreek Aug 02 '21 at 08:46
  • Thanks for your response. I also tried your code but getting the below error : HTTP Error 500.19 - Internal Server Error The requested page cannot be accessed because the related configuration data for the page is invalid. Added the error as screenshot – devram Aug 02 '21 at 09:29
  • Take a look at the Microsoft docs for this issue [here](https://learn.microsoft.com/en-us/troubleshoot/iis/http-error-500-19-webpage#hresult-code-0x80070021) where it says: > This problem can occur if the specified portion of the IIS configuration file is locked at a higher configuration level. There is a locking guide available [here](https://learn.microsoft.com/en-us/iis/get-started/planning-for-security/how-to-use-locking-in-iis-configuration) that should help you solve it. – jcreek Aug 02 '21 at 09:57