【问题标题】:Application_Error event in Global.asax not firing in IIS7, but works fine in IIS6Global.asax 中的 Application_Error 事件在 IIS7 中未触发,但在 IIS6 中工作正常
【发布时间】:2015-06-07 10:23:25
【问题描述】:

去年,我们将 Web 应用程序移至新服务器。以下是迁移前的系统/应用程序配置规范:

  • Windows Server 2003
  • IIS 6.0
  • ASP.NET 4.0
  • 网络表单

以下是移至新服务器后的规格:

  • Windows Server 2008
  • IIS 7.5
  • ASP.NET 4.5.1
  • WebForms/MVC 5 混合(感谢 VS 2013)

问题是,在环境因移动而发生剧烈变化后,Global.asax 中的 Application_Error 事件不再像以前那样触发。我遇到了很多关于此的问题(请参阅此问题的结尾),但似乎没有一个解决方案有效。它们也很老了,所以我认为 SE 应该提供一些关于这个主题的更新答案。

我想做什么:

如果抛出特定异常,我想导航到特定错误页面。否则,转到我的 web.config 中定义的error.aspx。自搬迁以来,我没有对 web.config 或代码进行任何更改。这是 Application_Error 事件:

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 在 web.config 中:

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

那么我需要做什么才能让我的应用程序在 IIS 7.5 中的行为与在 IIS 6 中的行为相同?

编辑:我会注意到在 localhost 下本地运行我的应用程序时会触发 Application_Error 事件。

我发现的其他问题在这里无法帮助我:

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

【问题讨论】:

    标签: asp.net asp.net-mvc-5 iis-7.5 iis-6 global-asax


    【解决方案1】:

    事实证明,这些答案毕竟是正确的:

    Application_Error does not fire?

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

    我最终将我的代码更改为:

    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
    

    在旧配置和新配置之间确实在框架方面发生了一些变化,因为这之前运行良好,因此我感到困惑。这很可能是 IIS 或 MVC 被引入 webforms 项目的事实。在这一点上我只能进行推理,但似乎在Application_Error 事件中调用Server.Transfer() 也有调用Server.ClearError() 的次要效果。但是,在新的环境中,情况不再如此。我敢打赌Server.Transfer() 确实会尝试导航到自定义页面,但是,由于在事件结束时错误没有被清除,ASP.NET 的默认错误处理进入并将用户带到@987654328 @。

    所以看起来这毕竟是一个愚蠢的问题,尽管对于经历剧烈环境变化并想知道为什么他们从未更改过的代码突然停止工作的其他人来说,保持这一点很重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-12
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多