【问题标题】:ASP C# Website Error HandlingASP C# 网站错误处理
【发布时间】:2017-07-09 08:04:01
【问题描述】:

在 Web.config 和 Index.aspx 中使用错误处理我正在使用 try and 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>

索引.aspx

try {

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

【问题讨论】:

  • 我假设您只想重定向到捕获中的“error.aspx”?如果是这样,你不能只做 Response.Redirect("Error.aspx") 吗?尽管您的问题有点不清楚,但也许您可以准确解释一下您要在 catch 中做什么?
  • 在catch { }中我想捕获错误并根据中的错误代码自动显示错误页面
  • 您将不得不编写一些代码。这不是通过将您的逻辑包装在 try and catch 或添加几行配置而自动发生的事情。这里解释了 web config 404 stackoverflow.com/questions/4483849/…

标签: c# asp.net


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 2021-01-25
    • 1970-01-01
    相关资源
    最近更新 更多