ASP C#网站错误处理

时间:2017-07-09 08:04:02

标签: c# asp.net

在Web.config和Index.aspx中使用错误处理我正在使用try和catch错误处理。如何在基于Web.config的Index.aspx catch {}中捕获错误。

的Web.config

<customErrors mode="On" defaultRedirect="Error.aspx">
<error statusCode="404" redirect="404.aspx" />
<error statusCode="500" redirect="500.aspx" />
</customErrors>

的Index.aspx

try {

}
catch {
How to trigger error handling in custom error?
}

1 个答案:

答案 0 :(得分:0)

defaultRedirect = "Error.aspx"表示当您有未处理的异常时,ASP.NET将调用您的Error.aspx页面而不是标准页面(称为“黄色死亡屏幕”)。

在我的情况下,我的行为如下:首先在Global.asax中我将未处理的异常存储在应用程序中:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when an unhandled error occurs
    Try
        Application("errore") = Server.GetLastError
    Catch ex As Exception
    end try
End Sub

然后在处理页面(在你的情况下,Error.aspx)我做处理:

Partial Class Error
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then

            If Session("userid") <> "" Then
                'Ho avuto una eccezione vera !
                Dim ex As Exception = Application("errore")
                Application("errore") = Nothing
                If IsNothing(ex) Then
                    ex = Server.GetLastError()
                End If
                If Not IsNothing(ex) Then
                    Dim bex As Exception = ex.GetBaseException
                    If IsNothing(bex) Then
                        bex = ex
                    End If

                    'Show details of exception bex on page or store in DB

                    Catch exall As Exception

                    End Try
                End If
            End If
        End If
    End If
End Sub
End Class