简体   繁体   English

Global.asax Application_Error无法在IIS上触发

[英]Global.asax Application_Error not firing on IIS

I have an ASP.NET MVC 2 page. 我有一个ASP.NET MVC 2页面。 During error or exception, the Application_Error method in Global.asax is not firing on IIS7. 在发生错误或异常期间,不会在IIS7上触发Global.asax中的Application_Error方法。

**But it does fire if I run the web app from VS2010 (localhost:someport) **但是如果我从VS2010(localhost:someport)运行Web应用程序,则确实会触发

My code (C#): 我的代码(C#):

protected void Application_Error(object sender, EventArgs e)
{
  Exception exception = Server.GetLastError();
  string requestedUrl = HttpContext.Current.Request.Url.ToString();

  Response.Clear();
  // do something
  Server.ClearError();
  Server.Redirect("Error");
}

My config: 我的配置:

<customErrors mode="On" defaultRedirect="~/Error">
</customErrors>

<system.webServer>
  <validation validateIntegratedModeConfiguration="false"/>
  <modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

Please help. 请帮忙。

This is because your web config settings are overriding the default behaviour. 这是因为您的Web配置设置将覆盖默认行为。 You will need to disable this (customErrors="Off" and remove the defaultRedirect (or set the correct status codes conditionally which allows unset errors to fall through to the Application_Error event). if you disable this, you can handle the error in the Application_Error event then redirect the user or display a message from this event. This web config setting is handling the errors and only unhandled errors will bubble up to this event. 您将需要禁用此设置(customErrors =“ Off”并删除defaultRedirect(或有条件地设置正确的状态代码,以使未设置的错误落入Application_Error事件)。如果禁用此设置,则可以处理Application_Error中的错误事件,然后重定向用户或显示该事件的消息此Web配置设置正在处理错误,只有未处理的错误才会冒泡到该事件。

One more thing. 还有一件事。
Application_Error isn't fired for exception that is raised in class/method that is marked HandleError attribute on IIS. 不会为IIS上标记为HandleError属性的类/方法中引发的异常触发Application_Error。
It fires if you run your app on asp.net development server but doesn't work for real IIS. 如果您在asp.net开发服务器上运行您的应用程序却会触发,但不适用于真正的IIS。 (I fell in this pitfall). (我陷入了这个陷阱)。

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

相关问题 Global.asax中的Application_Error自定义错误处理程序 - Application_Error custom error handler in Global.asax Starts..c中的Global.asax Application_Error等价物 - Global.asax Application_Error equivalent in Startup.cs Global.asax Application_Error NullReference 异常 - Global.asax Application_Error NullReference Exception global.asax 中的 Application_Error 未捕获 WebAPI 中的错误 - Application_Error in global.asax not catching errors in WebAPI global.asax Application_Error 在原始页面上报告 - global.asax Application_Error reporting on original page 在Global.asax文件中创建Application_Error处理程序,获取解析器错误 - Creating Application_Error handler in Global.asax file getting a Parser Error 获取引发Global.asax Application_Error中的最后一个错误的网页 - Get web page that threw the last error in Global.asax Application_Error Application_Error中的ASP.net Global.asax错误详细信息 - ASP.net Global.asax error detail in Application_Error ASP.NET MVC自定义错误处理Application_Error Global.asax? - ASP.NET MVC Custom Error Handling Application_Error Global.asax? 如何访问Global.ASAX中Application_Error事件的对象属性? - How to access object's properties of Application_Error event in Global.ASAX?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM