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

Zac*_*son 7 .net asp.net asp.net-mvc exception global-asax

这是我在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
Run Code Online (Sandbox Code Playgroud)

但是Server.Transfer失败了:

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

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

Jos*_*eph 9

我刚刚在Scott Hanselman的博客上找到了这个名为ELMAH的博客,它是一个错误记录器/处理程序,您无需更改代码即可使用.您可能想要研究它,因为它似乎与MVC很好地协作.

  • 我们在Stack Overflow上使用ELMAH的修改版本 - 它非常好. (7认同)
  • @Jarrod - 你们叉子里有什么类型的东西? (2认同)