Global.asax Application_Error not firing on IIS
I have an ASP.NET MVC 2 page. During error or exception, the Application_Error method in Global.asax is not firing on IIS7.
**But it does fire if I run the web app from VS2010 (localhost:someport)
My code (C#):
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Serve开发者_运维百科r.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. 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.
One more thing.
Application_Error isn't fired for exception that is raised in class/method that is marked HandleError attribute on IIS.
It fires if you run your app on asp.net development server but doesn't work for real IIS. (I fell in this pitfall).
精彩评论