【问题标题】:asp.net Custom error page not workingasp.net 自定义错误页面不起作用
【发布时间】:2016-05-03 09:58:57
【问题描述】:

我在 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 = "
违规网址:......

如果有办法捕捉错误行,在某些情况下我真的需要它...?

【问题讨论】:

标签: asp.net vb.net error-handling


【解决方案1】:

问题来自使用Iif,它同时评估真假情况。改用If() operator 可以防止这种情况发生。

您还应该在尝试使用之前检查ctxOBJ 是否为空,如果您在 If()s 中颠倒真假参数,它会使其更简单一点:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-06
    • 2012-07-24
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多