17

My global.asax seems not to be firing. I have:

void Application_Error(object sender, EventArgs e) 
{ 
    // Code that runs when an unhandled error occurs
    Server.Transfer("~/ExceptionFormView.aspx");
}

In my web.config, I don't have anything like CustomErrors. As I want everything to be handled by Global.asax and transferred to ExceptionFormView.aspx.

It works fine locally, but not when we deploy to servers. Any thoughts?

Do I need PrecompiledApp.config?

Patrick from NDepend team
  • 13,237
  • 6
  • 61
  • 92
  • Note that `HttpResponseException`s are ignored by `ApiExceptionHandler` and derived classes [by design](https://learn.microsoft.com/en-us/aspnet/web-api/overview/error-handling/exception-handling) and will never result in `Application_Error` firing. If you intend, for example, to log everything here, you'll need custom behaviour around those exceptions – user1007074 May 03 '23 at 08:01

4 Answers4

19

If you do not have a customErrors section in your Web.config, it is the same as having the section with mode="RemoteOnly". This customError mode will make local requests (made from the server running IIS) not use custom errors and it will execute the Application_Error() method as expected. Remote requests will use customErrors and not execute the method mentioned above.

This explains why you are seeing different behavior locally than on your server. You can reproduce this behavior on any environment by changing the customErrors mode to On or Off explicitly. On will not execute the Application_Error() section while Off will.

<customErrors mode="On|Off|RemoteOnly" />

This doesn't solve your problem of course, which is you want the method to be executed regardless. I have a bounty on another question where we are trying to figure out why the Application_Error() method is not firing when customErrors mode is On. Check back there in a couple days to see if we have found a solution.

Community
  • 1
  • 1
Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
4

If you previously deployed your application as precompiled but now you are not, then yes, you need to delete PrecompiledApp.config and your Deployment.dll in the bin directory. .NET will use the global code in Deployment.dll instead of your changes.

Josh Unger
  • 6,717
  • 6
  • 33
  • 55
  • This worked for me! I've been going around in circles for the past 6 hours, to just note that this was due to a precompilation! – Karl Cassar Sep 24 '13 at 13:43
  • My problem is the opposite. Previously, my application was a non-precompiled, but now it is pre-compiled. I verified that PrecompiledApp.config is in my root directory of my website, but my Application_Start() is still not firing. – Vin Shahrdar Oct 25 '17 at 20:42
4

If you are using IIS 7, try setting:

Response.TrySkipIisCustomErrors = true;
qbantek
  • 685
  • 12
  • 32
  • Reference : https://github.com/13daysaweek/Mvc4CustomErrorPage and http://blog.janjonas.net/2011-04-13/asp_net-prevent-iis_75_overriding-custom-error-page-iis-default-error-page – lahphim Jul 24 '14 at 08:58
  • Perfect, Thanks :) – mt025 Apr 07 '17 at 13:05
0

In IIS7, the application pool is integrated. It needs to be classic: http://praveenbattula.blogspot.com/2009/12/iis-7-managed-pipeline-mode-globalasax.html