ASP.NET MVC Override HandleError 导致视图无法渲染

发布于 2024-10-03 00:48:10 字数 1083 浏览 6 评论 0原文

在我的应用程序中,我使用“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

In my app I'm using "HandleError" whereby if an error happens, my "Error.vbhtml" view renders. This is working great, except now I want to also log the error. I've built a custom HandleError Class, Inherited the HandleErrorAttribute, and Overridden the OnException method.

Now my error gets logged, but the Error.vbhtml view doesn't get rendered... what praytell am I messing?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢华的一滴泪 2024-10-10 00:48:10

我刚刚在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

这似乎有效

I just took a look at the source code of the HandleError method at Codeplex. I scooped some of the code from there

        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

This appears to work

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文