我在IIS8.5上有一个自定义错误页面。有时,错误页面本身会抛出异常:
对象引用未设置为对象的实例。
这是我的代码背后的一部分:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
HttpContext.Current.Response.StatusCode = 500
'Dim CurrentException As Exception = Server.GetLastError()
'virheettxt.text = CurrentException.Message
Dim hostName = System.Net.Dns.GetHostName()
Dim ctxOBJ As HttpContext
Dim exceptionOBJ As Exception
Dim errorInfoTXT As String
ctxOBJ = HttpContext.Current()
exceptionOBJ = ctxOBJ.Server.GetLastError()
errorInfoTXT = " <br>Offending URL: " & iif(Not ctxOBJ Is Nothing, ctxOBJ.Request.Url.ToString(), "ei saatavilla") &
"<br>Source: " & iif(Not exceptionOBJ Is Nothing, exceptionOBJ.Source.ToString(), "ei saatavilla") &
"<br>Message: " & iif(Not exceptionOBJ Is Nothing, exceptionOBJ.Message.ToString(), "ei saatavilla") &
"<br>Stack trace: " & iif(Not exceptionOBJ Is Nothing, exceptionOBJ.StackTrace.ToString(), "ei saatavilla") &
"<br>Target Site: " & iif(Not exceptionOBJ Is Nothing, exceptionOBJ.TargetSite.ToString(), "ei saatavilla") &
"<br>Server: " & hostName
Dim virheurlsc = ctxOBJ.Request.Url.ToString()
ctxOBJ.Server.ClearError()
错误来自行:errorInfoTXT =“
违规网址:......
如果有办法捕获错误行,我在某些情况下也真的需要它......?
答案 0 :(得分:0)
问题来自于使用Iif
,它评估了真假案例。改为使用If() operator可以防止这种情况发生。
在尝试使用它之前,您还应该检查ctxOBJ
是否为空,如果您在If()中反转true和false参数,它将使它更简单:
Dim ctxOBJ As HttpContext = HttpContext.Current()
Dim exceptionOBJ As Exception = Nothing
If ctxOBJ IsNot Nothing Then
exceptionOBJ = ctxOBJ.Server.GetLastError()
End If
Dim errorInfoTXT As String = " <br>Offending URL: " & If(ctxOBJ Is Nothing, "ei saatavilla", ctxOBJ.Request.Url.ToString()) &
"<br>Source: " & If(exceptionOBJ Is Nothing, "ei saatavilla", exceptionOBJ.Source.ToString()) &
"<br>Message: " & If(exceptionOBJ Is Nothing, "ei saatavilla", exceptionOBJ.Message.ToString()) &
"<br>Stack trace: " & If(exceptionOBJ Is Nothing, "ei saatavilla", exceptionOBJ.StackTrace.ToString()) &
"<br>Target Site: " & If(exceptionOBJ Is Nothing, "ei saatavilla", exceptionOBJ.TargetSite.ToString()) &
"<br>Server: " & hostName