2016-05-03 35 views
0

我在IIS8.5上有自定义错误页面。有时错误页面本身会引发异常:asp.net自定义错误页面不起作用

未将对象引用设置为对象的实例。

这背后是我的代码部分:

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

请参阅[将行号添加到在发布模式下部署的ASP.NET网站的堆栈跟踪](http://stackoverflow.com/a/20069668/1115360),以获得第二个问题。 –

+0

我该怎么做? – Timo77

+0

我认为我提到的答案告诉你。 –

回答

0

问题来自使用Iif,它可以评估真实和虚假情况。使用If() operator反而会阻止这一点。

您也应该检查是否ctxOBJ无非试图使用它之前,如果你扭转的。如果真和假的论点()■这将使它变得更加简单:

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 
相关问题