2010-11-16 2 views
1

В моем приложении я использую «HandleError», в результате чего при возникновении ошибки отображается мой «Error.vbhtml». Это отлично работает, но теперь я хочу также зарегистрировать ошибку. Я создал собственный класс HandleError, унаследовал HandleErrorAttribute и переопределил метод OnException.ASP.NET MVC Override 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()' после того, как вы закончите регистрацию событий? [shot-in-the-dark], также 'HandleError' не лучший выбор имени. Атрибуты всегда должны заканчивать имя словом «Атрибут» и, возможно, не передавать имена с классом, который они наследуют (даже если они находятся в другом пространстве имен). –

ответ

1

Я просто взглянул на исходный код метода HandleError в Codeplex. Я зачерпнул часть кода оттуда

 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 

Это, кажется, работает

Смежные вопросы