如何在 ASP.NET MVC 中记录未处理的异常?
-
03-07-2019 - |
题
这是我在 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.
我该如何解决?或者,对我来说更好的方法是什么?
其他提示
您必须指定一个虚拟路径,即相对于应用程序库的路径(即应用程序外部没有路径),所以类似于: Server.Transfer的(QUOT;&的error.html QUOT) 要么 Server.Transfer的(QUOT; /error.html") 要么 Server.Transfer的(QUOT;〜/&的error.html QUOT)
默认情况下,ASP.NET MVC应用程序具有继承自
的视图Shared / Error.aspxSystem.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>
如果您的控制器使用属性[HandleError],则所有异常将继续冒泡直到被捕获,并且它们将最终在该页面上。
我只是添加了一个内联的Page_Load(在这种情况下有效,因为它是该行的结尾):
<script runat="server">
Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)
MyExceptionHandlerService.LogException("exceptionsource", this.Model.Exception)
End Sub
</script>
之后,友好的“抱歉......”信息。看起来ELMAH看起来更加强大,但是根据我的需要,这已经足够了。
不隶属于 StackOverflow