Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

520
Views
Handle Custom error using defaultpath not working in c# Webconfig?

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!