简体   繁体   中英

Application_Error event in Global.asax not firing in IIS7, but works fine in IIS6

Last year, we moved our web applications to a new server. Here are the system/application configuration specs prior to the move:

  • Windows Server 2003
  • IIS 6.0
  • ASP.NET 4.0
  • WebForms

Here are the specs after moving to the new server:

  • Windows Server 2008
  • IIS 7.5
  • ASP.NET 4.5.1
  • WebForms/MVC 5 hybrid (thanks to VS 2013)

The problem is that, after the drastic changes in environment due to the move, the Application_Error event in Global.asax is no longer firing like it was before. I've encountered a number of questions on this (see end of this question), but none of the solutions appear to work. They are also quite old, so I think SE is due for some updated answers on this topic.

What I want to do:

If a specific exception is thrown, I want to navigate to a specific error page. Otherwise, proceeds to error.aspx as defined in my web.config. I've made no changes to the web.config or code since the move. Here's the Application_Error event:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

    If TypeOf (Server.GetLastError) Is System.Web.HttpRequestValidationException Then
        NavigateToErrorPage("Display special error message here")
    End If

End Sub

customErrors in web.config:

<customErrors mode="RemoteOnly" defaultRedirect="error.aspx" />

So what do I need to do to get my application to behave in IIS 7.5 the same way it behaved in IIS 6?

Edit : I'll note that the Application_Error event fires when running my application locally under localhost.

Other questions I've found that were unable to assist me here:

Application_Error event global.asax not getting triggered

global.asax Application_Error not firing

Application_Error not firing when customerrors = "On"

Is global.asax Application_Error event not fired if custom errors are turned on?

Application_Error does not fire?

Global.asax not firing for .aspx pages in IIS7

It turns out that these answers are correct after all:

Application_Error does not fire?

Is global.asax Application_Error event not fired if custom errors are turned on?

I ended up changing my code to this:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

    Dim ex As Exception = Server.GetLastError

    If TypeOf ex Is System.Web.HttpRequestValidationException Then
        Server.ClearError()
        'NavigateToErrorPage() calls Server.Transfer()

        NavigateToErrorPage("Display special error message here")
    End If

End Sub

Something certainly changed framework-wise between the old configuration and the new because this was working fine before, hence my confusion. It's most likely either IIS or the fact that MVC was introduced into the webforms project. I can only theorize at this point, but it seems as though calling Server.Transfer() within the Application_Error event also had the secondary effect of calling Server.ClearError() . However, in the new environment, this is no longer the case. I am betting that Server.Transfer() does indeed attempt to navigate to the custom page, but, since the error isn't being cleared by the end of the event, ASP.NET's default error handling comes in and whisks the user away to error.aspx .

So it appears as though this is a dupe question after all, though it's important to keep around for anyone else who undergoes drastic environment changes and wonders why code they never made changes to has suddenly stopped working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM