gpt4 book ai didi

asp.net 自定义错误页面不起作用

转载 作者:行者123 更新时间:2023-12-03 08:52:42 26 4
gpt4 key购买 nike

我在 IIS8.5 上有一个自定义错误页面。有时错误页面本身会抛出异常:

Object reference not set to an instance of an object.



这是我背后的代码的一部分:
    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 = "违规网址:......

如果有一种方法可以捕获错误行,我在某些情况下也确实需要它......?

最佳答案

问题来自于使用 Iif ,它评估真实和错误的情况。使用 If() operator相反会阻止这种情况。

您还应该检查是否 ctxOBJ在尝试使用它之前什么都不是,如果你颠倒 If()s 中的 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

关于asp.net 自定义错误页面不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37001082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com