2010-11-16 60 views
1

在我的應用程序中,我使用「HandleError」,如果發生錯誤,我的「Error.vbhtml」視圖呈現。這很好,除了現在我還想記錄錯誤。我已經構建了一個自定義的HandleError類,繼承了HandleErrorAttribute,並重寫了OnException方法。ASP.NET MVC覆蓋HandleError導致查看不呈現

現在我的錯誤得到記錄,但Error.vbhtml視圖不會得到呈現......我在搞什麼祈禱?

Imports System.Web.Mvc 

Namespace Mvc.Attributes 
    Public Class HandleError : Inherits System.Web.Mvc.HandleErrorAttribute 
     Private ExceptionService As Domain.IExceptionService 
     Public Sub New() 
      ExceptionService = New Domain.ExceptionService(New Domain.ExceptionRepository) 
     End Sub 

     Public Overrides Sub OnException(ByVal exceptionContext As ExceptionContext) 
      ''# Log the exception if it has not been handled elsewhere 
      If Not exceptionContext.ExceptionHandled Then 
       ExceptionService.AddException(exceptionContext.Exception) 
       ExceptionService.SubmitChanges() 
       ''# Signal to the system that we've handled the exception 
       exceptionContext.ExceptionHandled = True 
      End If 
     End Sub 
    End Class 
End Namespace 
+0

嘗試調用'base.OnException()'你完成之後登錄? [黑暗中射擊],另外'HandleError'不是有史以來最好的選擇。屬性應該始終以名稱「Attribute」結尾,並且可能不會與它們繼承的類共享名稱(即使它們位於不同的名稱空間中)。 – 2010-11-16 22:05:47

回答

1

我只看了一下Codeplex的HandleError方法的源代碼。我舀一些代碼從那裏

 Dim controllerName As String = DirectCast(filterContext.RouteData.Values("controller"), String) 
     Dim actionName As String = DirectCast(filterContext.RouteData.Values("action"), String) 
     Dim model As New HandleErrorInfo(filterContext.Exception, controllerName, actionName) 
     filterContext.Result = New ViewResult() With { _ 
     .ViewName = View, _ 
     .MasterName = Master, _ 
     .ViewData = New ViewDataDictionary(Of HandleErrorInfo)(model), _ 
     .TempData = filterContext.Controller.TempData _ 
     } 
     filterContext.ExceptionHandled = True 
     filterContext.HttpContext.Response.Clear() 
     filterContext.HttpContext.Response.StatusCode = 500 

     ''# Certain versions of IIS will sometimes use their own error page when 
     ''# they detect a server error. Setting this property indicates that we 
     ''# want it to try to render ASP.NET MVC's error page instead. 
     filterContext.HttpContext.Response.TrySkipIisCustomErrors = True 

這似乎工作