How to catch errors resulting from a bad web.config?

1.3k Views Asked by At

This is for an ASP.NET app. I get a runtime error when attempting to load the application because of something I have in the web.config file that I'm missing. I would like to get the custom error page to display, but I'm afraid that if there is an error in the web.config, then the customerror attribute won't work. Is that right?

In the web.config, I have this:

<system.web>
<customErrors configSource="config\customErrors.config" />
</system.web>

In my customError.config file, I have this:

<?xml version="1.0"?>
<customErrors defaultRedirect="~/ErrorPages/DefaultError.html" mode="On"/>

I also added the authorization attributes thinking I would need it. I removed it since it seems to make no difference.

<location path="ErrorPages/DefaultError.html">
<system.web>
  <authorization>
    <allow users="*" />
  </authorization>
</system.web>
</location>
<location path="Config/customErrors.config">
<system.web>
  <authorization>
    <allow users="*" />
  </authorization>
</system.web>
</location>

I added more location paths for the stylesheets and images.

This is the runtime error: Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed.

And it asks me to set my customerrors to off or remote only, and to create a customerror page with mode to On, which I already have.

When the mode is Off, then the error is this: Could not load file or assembly 'AntiXSSModule' or one of its dependencies. The system cannot find the file specified. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

My customerror page works just fine when I'm logged into the application and also before login. I would also like to be able to cath these types of errors. This is the line that I should not have in the web.config since I'm missing some files/references, but instead of crashing, I would like to be able to show the customerror page.

<add name="ValidateInput" type="AntiXSSModule.ValidateInput, AntiXSSModule"/>

I've tried to move my location path around, thinking that would matter, putting it above and before the line that gives me the error. I looked at question CustomErrors mode="Off", which is the same error I'm getting, but adding the attribute did not fix the problem, I get an error with the config file when I add this.

The question is how can I catch an error that is resulting from an error in web.config file? I haven't tried this yet, but from looking at various questions I saw I can add method to my global.asax; either Application_Exception or Application_Error. Currently I only have the methods Application_Start and Application_End.

I'm new to ASP.NET and would like to this the right away, any suggestions?

1

There are 1 best solutions below

3
On

You should check the server's Event Viewer in its Application log to see why the application cannot load.

Generally for unhandled errors, i.e. exceptions that the application could anticipate, you can use ELMAH for logging and notifying you of errors.

For exceptions that are handled within an application, i.e. within a try/catch, you can use log4net for logging and notifying you of errors.

Both these libraries can log exceptions to a database, or even email you the exceptions.

Update: To be clear about this (given that the question is how can I catch an error that is resulting from an error in web.config file?), if your application config is invalid the application will not load and therefore will not be able to serve pages let alone error pages. You should not be considering how to catch application configuration errors but instead be figuring out how to make your application configuration valid, then you won't have this issue.