如何在 ASP.NET MVC 中记录未处理的异常?

2023-12-27

这是我在 Global.asax.vb 中尝试执行的操作:

Public Class MvcApplication
    Inherits System.Web.HttpApplication

    Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
        routes.MapRoute( _
            "Error", _
            "error.html", _
            New With {.controller = "Error", _
                      .action = "FriendlyError"} _
        )
        ...
        'other routes go here'
        ...
    End Sub

    Sub Application_Start()
        RegisterRoutes(RouteTable.Routes)
    End Sub

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ...
        'code here logs the unhandled exception and sends an email alert'
        ...
        Server.Transfer("http://www.example.com/error.html")
    End Sub

End Class

但是 Server.Transfer 失败了:

Invalid path for child request 'http://www.example.com/error.html'. A virtual path is expected.

我该如何解决?或者,对我来说更好的方法是什么?


我刚刚在 Scott Hanselman 的博客上找到了这个here http://www.hanselman.com/blog/ELMAHErrorLoggingModulesAndHandlersForASPNETAndMVCToo.aspx called ELMAH http://www.raboof.com/projects/Elmah/这是一个错误记录器/处理程序,您可以使用它而无需更改代码。您可能想研究一下它,因为它似乎与 MVC 配合得很好。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 ASP.NET MVC 中记录未处理的异常? 的相关文章

随机推荐