Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to check, programmatically, if an ASP.NET application's CustomErrors are set to Off?

Tags:

We usually catch unhandled exceptions in Global.asax, and then we redirect to a nice friendly error page. This is fine for the Live environment, but in our development environment we would like to check if CustomErrors are Off, and if so, just throw the ugly error.

Is there an easy way to check if CustomErrors are Off through code?

like image 210
willem Avatar asked Aug 18 '10 14:08

willem


People also ask

Where do you put customErrors mode off?

The customErrors tag must be placed within tags. Create a <customErrors> tag within a "web. config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

What is default mode of the Customerror section?

CustomErrors supports the following modes: On – If defaultRedirect is specified, they will see that content. Otherwise, the default error screen with fewer details. Off – Detailed error details will be shown to the user.

How do I turn off custom error mode?

Choose the ASP.NET tab. Click on "Edit Configuration". Click the Custom Errors tab. Select Off for custom error mode.

How does ASP.NET handle custom errors?

In ASP.Net, error can be handled programmatically by writing appropriate code in the page-level error event, for errors on an individual page or in the application-level error event for handling errors that may occur in any page of the application.


1 Answers

I would suggest using the following property:

HttpContext.Current.IsCustomErrorEnabled 

As mentioned here, IsCustomErrorEnabled takes more things like RemoteOnly into consideration:

The IsCustomErrorEnabled property combines three values to tell you whether custom errors are enabled for a particular request. This isn't as simple as reading the web.config file to check the section. There's a bit more going on behind the scenes to truly determine whether custom errors are enabled.

The property looks at these three values:

  1. The web.config's < deployment > section's retail property. This is a useful property to set when deploying your application to a production server. This overrides any other settings for custom errors.

  2. The web.config's < customErrors > section's mode property. This setting indicates whether custom errors are enabled at all, and if so whether they are enabled only for remote requests.

  3. The HttpRequest object's IsLocal property. If custom errors are enabled only for remote requests, you need to know whether the request is from a remote computer.

like image 192
kaptan Avatar answered Sep 30 '22 15:09

kaptan