2009-09-17 37 views

回答

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"); 
            
        } 
    } 
} 
+0

我最終做了類似於你的東西只是發佈,除了代替Response.Redirect,我使用了Server.Transfer(「CustomErrorPage.aspx」,true)。我們有太多頁面需要重定向到錯誤頁面,所以我們只有一個顯示消息的錯誤頁面。謝謝! – Pwninstein 2009-09-18 21:48:34

+0

很高興我能幫到你。我們將response.redirect用於錯誤頁面,因爲在某些情況下,我們似乎在server.transfer方面存在一些問題。 – Jim 2009-09-21 13:31:47

0

你可以使用web.config中的自定義錯誤as explained here

+0

我沒有想到的是,雖然,沒有按自定義錯誤集合只適用於與HTTP相關的錯誤(因爲每個錯誤元素都有一個StatusCode屬性)? – Pwninstein 2009-09-17 17:00:15

+0

我認爲defaultRedirect屬性處理所有的錯誤,但我可能是錯的。這個問題似乎回答你最初的查詢:http://stackoverflow.com/questions/1227632/asp-net-2-0-best-practice-for-writing-error-page – Locksfree 2009-09-17 17:11:28

相關問題