0

Global.asax で、SQL タイムアウトをエレガントに処理し、要求ページにエラーを説明するメッセージを表示する方法はありますか? Global.asax に Application_Error イベントと Error イベントがあることは知っていますが、これを実現するためにどちらを使用できるか (もしあれば) はわかりません。

関連して、Global.asax が処理しているエラーが発生したページ インスタンスにアクセスできますか?

ありがとう!

4

2 に答える 2

1

global.asax.vb 内

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)

        If TypeOf (Context.Error.GetBaseException) Is System.Data.SqlClient.SqlException Then

            Dim ex As System.Data.SqlClient.SqlException = Context.Error.GetBaseException

            If (ex.Errors.Count = 1 And ex.Number = -2) Then

                Server.ClearError()

                Response.Redirect(Context.Request.Url.ToString() & "?error=MessageHere")

            End If


        End If

End Sub

C# の場合

public void Application_Error(object sender, EventArgs e)
{
    if ((Context.Error.GetBaseException) is System.Data.SqlClient.SqlException) {
        
        System.Data.SqlClient.SqlException ex = Context.Error.GetBaseException;
        
        if ((ex.Errors.Count == 1 & ex.Number == -2)) {
            
            Server.ClearError();
                
            Response.Redirect(Context.Request.Url.ToString() + "?error=MessageHere");
            
        }
    }
}
于 2009-09-18T14:08:07.700 に答える
0

ここで説明されているように、web.config カスタム エラーを使用できます。

于 2009-09-17T16:54:20.903 に答える