代码之家  ›  专栏  ›  技术社区  ›  Chase Florell

ASP.NET MVC重写句柄错误导致视图无法呈现

  •  1
  • Chase Florell  · 技术社区  · 15 年前

    在我的应用程序中,我使用“HandleError”,如果发生错误,我的“error.vbhtml”视图将呈现。这是很好的工作,除了现在我想也记录错误。我已经构建了一个自定义HandleError类,继承了HandleErrorAttribute,并重写了OnException方法。

    现在我的错误被记录,但是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
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   Chase Florell    15 年前

    我刚刚看了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
    

    这似乎有效

    推荐文章