简体   繁体   English

Global.asax中的Application_Error事件在IIS7中未触发,但在IIS6中工作正常

[英]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. 去年,我们将Web应用程序移到了新服务器上。 Here are the system/application configuration specs prior to the move: 以下是迁移之前的系统/应用程序配置规格:

  • Windows Server 2003 Windows Server 2003
  • IIS 6.0 IIS 6.0
  • ASP.NET 4.0 ASP.NET 4.0
  • WebForms Web表格

Here are the specs after moving to the new server: 以下是移至新服务器后的规格:

  • Windows Server 2008 Windows Server 2008
  • IIS 7.5 IIS 7.5
  • ASP.NET 4.5.1 ASP.NET 4.5.1
  • WebForms/MVC 5 hybrid (thanks to VS 2013) WebForms / MVC 5混合(由于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. 问题在于,由于移动导致环境发生了巨大变化之后,Global.asax中的Application_Error事件不再像以前那样触发。 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. 它们也很老,所以我认为SE应该提供有关此主题的一些更新答案。

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. 否则,请转到我的web.config中定义的error.aspx I've made no changes to the web.config or code since the move. 自从移动以来,我没有对web.config或代码进行任何更改。 Here's the Application_Error event: 这是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 in web.config: web.config中的customErrors

<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? 那么,我该怎么做才能使应用程序在IIS 7.5中的行为与在IIS 6中的行为相同?

Edit : I'll note that the Application_Error event fires when running my application locally under localhost. 编辑 :我会注意到在本地下本地运行我的应用程序时会触发Application_Error事件。

Other questions I've found that were unable to assist me here: 我发现的其他问题在这里无法帮助我:

Application_Error event global.asax not getting triggered 没有触发Application_Error事件global.asax

global.asax Application_Error not firing global.asax Application_Error无法启动

Application_Error not firing when customerrors = "On" 当customerrors =“ On”时,Application_Error不触发

Is global.asax Application_Error event not fired if custom errors are turned on? 如果打开自定义错误,是否会触发global.asax Application_Error事件?

Application_Error does not fire? Application_Error不触发?

Global.asax not firing for .aspx pages in IIS7 Global.asax无法在IIS7中为.aspx页触发

It turns out that these answers are correct after all: 事实证明,这些答案毕竟是正确的:

Application_Error does not fire? Application_Error不触发?

Is global.asax Application_Error event not fired if custom errors are turned on? 如果打开自定义错误,是否会触发global.asax Application_Error事件?

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. 很有可能是IIS,也可能是MVC被引入到Webforms项目中。 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() . 我现在只能进行理论化,但是似乎在Application_Error事件中调用Server.Transfer()也具有调用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 . Server.Transfer()确实确实尝试导航到自定义页面,但是,由于该错误并未在事件结束时被清除,因此ASP.NET的默认错误处理进来了,这使用户无法进入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. 因此,看来这毕竟是一个愚蠢的问题,尽管重要的是要让其他任何经历过剧烈环境变化的人都留在身边,并且想知道为什么他们从未更改过的代码突然停止工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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