2

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?

Community
  • 1
  • 1
L R
  • 21
  • 2
  • Maybe this can help: [ASP.NET custom error page - Server.GetLastError() is null](http://stackoverflow.com/questions/343014/asp-net-custom-error-page-server-getlasterror-is-null) – Thomas C. G. de Vilhena Aug 12 '12 at 18:02
  • The errors all always recorded on Event Viewer. – Aristos Aug 12 '12 at 18:04
  • Ever got this to work? – Michel Sep 29 '16 at 09:53
  • @Michael It would never be possible to get round this issue, since if the application configuration is invalid, the application cannot load. If it cannot load it cannot serve pages including error pages. Strictly speaking, it should be considered very bad practice to even try and code around invalid application configuration. The application configuration should *always* be valid and be as static as possible. – Digbyswift Oct 06 '16 at 08:38

1 Answers1

0

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.

Digbyswift
  • 9,945
  • 2
  • 36
  • 65
  • Ok, but he asked how he could load a custom error page in the event that this kind of error happens, where the `customErrors` doesn't work. – wired_in Feb 24 '14 at 02:24
  • this is not an answer to the question – Michel Sep 29 '16 at 09:52
  • It certainly is :) The question title is `How to catch errors resulting from a bad web.config?` and the OP explicitly states `The question is how can I catch an error that is resulting from an error in web.config file?` which I think my answer covers sufficiently. – Digbyswift Oct 06 '16 at 08:30