Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom error handling for a specific page in ASP.Net

On a site I run, I have 404's and 500 errors mapped to redirect to a custom error page for end users; using the following code in my web.config:

 ...
 ...

 <system.web>
  <customErrors defaultRedirect="/404/default.aspx" mode="RemoteOnly">

     <error statusCode="404" redirect="/404/default.aspx" />

  </customErrors>

  ...
  ...

However, I have one specific page that I do not want redirected; a health check page to make sure the site is 100% operational. I tried setting a location specific custom error handler using the code below in my web.config:

 ...
 ...

   </system.web>
  <location path="health.aspx">
    <system.web>
      <customErrors defaultRedirect="" mode="RemoteOnly"></customErrors>
    </system.web>
  </location>
 ...
 ...

However, it doesn't seem to work. When I rename health.aspx to something else like badhealth.aspx, then make a request, I expect to get a generic 404 error via the Yellow Screen of Death page. Similarly, by intentionally changing the code to throw an error, I should get 500 error via the YSOD page. In both cases, I end up being redirected to our custom 404 page, rathen then getting a YSOD. Thoughts?

Any assistance is greatly appreciated.

Thanks all, - Frank

like image 832
Frank Rosario Avatar asked Apr 20 '09 17:04

Frank Rosario


People also ask

How do you implement a custom error page?

The <customErrors> section in Web. config has two attributes that affect what error page is shown: defaultRedirect and mode . The defaultRedirect attribute is optional. If provided, it specifies the URL of the custom error page and indicates that the custom error page should be shown instead of the Runtime Error YSOD.

Which tag is used for providing custom error pages for the ASP.NET http errors?

You can use the <customErrors> in web. config to display custom pages when exceptions occurred in MVC application with the response status code 200. For SEO reason, you may want to display a custom error page and return an appropriate error code in ASP.NET webform or MVC application.

Which is used for configuring the custom error page to custom error code in web config file?

The <customErrors> element under system. web in web. config is used to configure error code to a custom page. It can be used to configure custom pages for any error code 4xx or 5xx.


1 Answers

Nevermind, we solved the issue. It seems when you have a location specific customError directive; if you leave the defaultRedirect as "", it will default to the sitewide defaultRedirect URL.

The solution was simply to turn off CustomError handling for this specific path, like so:

<system.web>
    <customErrors defaultRedirect="/404/default.aspx" mode="RemoteOnly">
        <error statusCode="404" redirect="/404/default.aspx" />
    </customErrors>
</system.web>
<location path="health.aspx">
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</location>

Thanks anyway, Jose.

like image 100
Frank Rosario Avatar answered Nov 15 '22 05:11

Frank Rosario