HttpContext.Current.Server.GetLastError在IIS 7中不工作

问题描述:

我只是试图在错误页面上输出应用程序错误的详细信息。在global.asax我有一些处理,然后一个server.transfer到我的网页与此代码:HttpContext.Current.Server.GetLastError在IIS 7中不工作

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
    'Get exception details 
    Dim ex As Exception = HttpContext.Current.Server.GetLastError() 

    If TypeOf ex Is HttpUnhandledException AndAlso ex.InnerException IsNot Nothing Then 
     ex = ex.InnerException 
    End If 

    If ex Is Nothing Then 
     lblError.Text = "<p>An unknown error occurred.</p>" 
    Else 
     'Determine what type of error we're dealing with 
     If TypeOf ex Is System.Data.SqlClient.SqlException Then 
      'A database-related exception... 
      lblError.Text = String.Format("<p>The database is having problems... please try your submission again later.<br />{0}</p>", ex.Message) 
     ElseIf TypeOf ex Is ApplicationException Then 
      'An ApplicationException has occurred 
      lblError.Text = String.Concat("<p>An application fault has been tripped:<br /> {0}</p>", ex.Message) 
     Else 
      'For all others, display the StackTrace 
      lblError.Text = String.Format("<p>An unknown error occurred. The following dump of exception information can be used to diagnose the issue</p><p class=""stackTrace""><strong>Exception:</strong> {0}</p><p class=""stackTrace""><strong>Message:</strong> {1}</p><p class=""stackTrace""><strong>Stack Trace:</strong><br />{2}</p>", ex.GetType().ToString(), ex.Message, ex.StackTrace.Replace(System.Environment.NewLine, "<br />")) 
     End If 
    End If 

End Sub 

本地它工作的很好。当我部署到运行IIS 7的Windows Server 2008计算机时,ex的条件不会每次都被触发。 我猜这个问题与IIS版本有关 - 只是因为我看到了一些其他帖子,但没有解决方案。如果有人对我如何完成错误消息转储有想法,我会很感激。我想知道为什么这也发生。我的意思是,一个解决方案是伟大的,但我很好奇为什么。

有在 Vista中的一个非常相似的已知错误,这是所谓固定在 SP1,但它看起来像相同的修订 是不能在Windows 2008 Server的一部分呢。 有一种变通方法,但 - 如果你 设置网站的默认错误财产 (下IIS设置 - >错误页面 - > 编辑功能设置...),以自定义 页面(见下文),IIS将调用此 页每当错误不被明确配置 状态码处理(所以你的404,等 处理程序仍然可以工作)处理 - 但 某些原因,处理错误这 方式意味着服务器.GetLastError()仍然 正常工作。

查看原文博文:Fun with Server.GetLastError() in classic ASP on Windows Server 2008

我想它与您的情况类似,也可能对您有所帮助。

另一种方法是在web.config中使用 ASP.NET 3.5 SP1中引入的新参数redirectMode

redirectMode="ResponseRewrite" 模式允许加载错误页面 不重定向浏览器,网址 保持不变,而异常将 不会丢失。

这是answeredASP.NET自定义错误页 - Server.GetLastError()为null问题。查看更多细节。