2017-07-09 8 views
0

Web.configとIndex.aspxでエラー処理を使用していますが、私はtryとcatchのエラー処理を使用しています。 Web.configに基づいてIndex.aspx catch {}のエラーをキャッチする方法ASP C#Webサイトのエラー処理

の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? 
} 
+0

catchの「error.aspx」にリダイレクトしたいと思っていますか?もしそうなら、あなたは単にResponse.Redirect( "Error.aspx")を実行できませんでしたか?あなたの質問はちょっと不明かもしれませんが、あなたがキャッチで何をしようとしているのか正確に説明できますか? – JKerny

+0

catch {}でエラーをキャッチして、のエラーコードに基づいてエラーページを自動的に表示します。 – Syy

+0

いくつかのコードを記述する必要があります。これはあなたのロジックを試してラップしたり、数行の設定を追加したりすることで自動的に起こるものではありません。ここでは、Web設定404について説明します。https://stackoverflow.com/questions/4483849/default-redirect-for-error-404 – JKerny

答えて

0

defaultRedirect = "Error.aspx"はあなたが未処理の例外を持っている場合、ASP.NETは、(標準の代わりにあなたのError.aspxページを呼び出すことを意味します「死の黄色いスクリーン」と呼ばれる)。私の場合は

私はこのように振る舞う:(あなたの場合、Error.aspxに)処理ページに続いて

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 

私は取扱いを行います:最初のGlobal.asaxに私は、アプリケーションで未処理の例外を保存

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 
関連する問題