[英]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.