【问题标题】:redirect to error page when Exception occured in asp.net?在 asp.net 中发生异常时重定向到错误页面?
【发布时间】:2011-08-07 13:48:01
【问题描述】:

我想在发生异常时重定向到错误页面。

如果代码未处理页面中的try{}catch(Exception ex){},并且Web 应用程序中发生错误。然后我想将带有异常详细信息的 Error.aspx 重定向到显示。

我已经在 Global.asax 页面中写过这样的代码。

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    ' Code that runs when an unhandled error occurs
    Dim str As String = CType(sender, HttpApplication).Context.Request.Url.ToString
    Helper.ApplicationErrorHandler()
End Sub

Public Shared Sub ApplicationErrorHandler()
    Dim ex As Exception

    ex = HttpContext.Current.Server.GetLastError

    Dim str As String = HttpContext.Current.Request.Url.ToString()

    ' filter out 404 responses 
    Dim httpException = TryCast(ex, HttpException)
    If httpException IsNot Nothing AndAlso httpException.GetHttpCode() = 404 Then
        Return
    End If


    If TypeOf ex Is HttpUnhandledException AndAlso ex.InnerException IsNot Nothing Then
        ex = ex.InnerException
    End If

    If (ex IsNot Nothing) Then

        Dim msg As String = String.Format("An unhandled exception occurred:{0}Message: {1}{0}{0} Stack Trace:{0}{2}", System.Environment.NewLine, ex.Message, ex.StackTrace)
        Utility.SendEMail("LaborDB@alixpartners.com", System.Configuration.ConfigurationManager.AppSettings("ErrorEmailRecipient"), msg, "Labor Error")

    End If
    HttpContext.Current.Session.Add("Error", ex.Message)
    HttpContext.Current.Server.Transfer("error.aspx")

End Sub

仍然是仅在错误位置停止的异常。它没有重定向到错误页面。

    Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   Labor.AttributeProcess.Page_Load(Object sender, EventArgs e) in C:\Source Trunk\Labor\Labor\AttributeProcess.aspx.vb:23
   System.Web.UI.Control.OnLoad(EventArgs e) +131
   System.Web.UI.Control.LoadRecursive() +65
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2427

如何处理?即使没有 try.. catch 句柄,我也想在发生异常时重定向到错误页面。

【问题讨论】:

    标签: asp.net vb.net exception exception-handling


    【解决方案1】:

    查看 web.config 中的 customErrors Element。您可以使用它来配置您的网站以在出现异常时重定向到其他页面。

    【讨论】:

      【解决方案2】:

      Jakob Gade 建议的链接不再有效,所以如果有人像我一样再次遇到同样的问题,请在 global.asax 中的 Application_Error 方法中复制以下代码:

      // After Handle the error. Clear It
      Server.ClearError();
      
      // Clear rendered. Just if needed
      Response.Clear();
      
      // Finally redirect, transfer
      Response.Redirect("~/ErrorViewer.aspx");
      

      【讨论】:

        猜你喜欢
        • 2015-01-04
        • 2013-04-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-20
        • 1970-01-01
        • 2017-07-08
        相关资源
        最近更新 更多