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?
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".
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.
Choose the ASP.NET tab. Click on "Edit Configuration". Click the Custom Errors tab. Select Off for custom error mode.
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.
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:
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With