如何在 ASP.NET MVC 中记录未处理的异常?
这是我在 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 失败了:
子请求“http://www.example.com/error.html”的路径无效。 需要一个虚拟路径。
我该如何解决这个问题? 或者,对我来说更好的方法是什么?
Here's what I'm trying to do in my 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
But that Server.Transfer fails:
Invalid path for child request 'http://www.example.com/error.html'. A virtual path is expected.
How do I fix this? Or, what's a better way for me to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我刚刚在 Scott Hanselman 的博客 此处 上找到了这个,名为 ELMAH 这是一个错误记录器/处理程序,您无需更改代码即可使用。 您可能想研究一下它,因为它似乎与 MVC 配合得很好。
I just found this on Scott Hanselman's blog here called ELMAH which is an Error Logger/Handler that you can use without having to change your code. You might want to look into it, since it seems to work nicely with MVC.
ELMAH 是一个出色的错误处理程序,用于捕获未处理的异常。 它通过 HttpModule 无缝插入您的 Web 应用程序,并具有各种通知和日志记录选项。
功能:
仅供参考,SO 使用 ELMAH,尽管是一个分叉版本。 这是最好的架构解释和设置教程
ELMAH is an excellent error handler for catching unhandled exceptions. It plugs seamlessly into your web application via HttpModules and has various options for notification and logging.
Features:
And FYI, SO uses ELMAH, albeit a forked version. This is the best architectural explanation and setup tutorial
您必须指定一个虚拟路径,即相对于应用程序基础的路径(即应用程序外部没有路径),因此类似于:
Server.Transfer("error.html")
或者
Server.Transfer("/error.html")
或者
Server.Transfer("~/error.html")
You have to specifiy a virtual path, i.e. a path relative to the application base (i.e. no paths external to the app), so something like:
Server.Transfer("error.html")
or
Server.Transfer("/error.html")
or
Server.Transfer("~/error.html")
默认情况下,ASP.NET MVC 应用程序有一个视图 Shared/Error.aspx,继承自
如果您的控制器使用属性 [HandleError],则所有异常将继续冒泡,直到被捕获,并且它们最终会出现在该页面上。
我只是添加了一个内联 Page_Load(在本例中有效,因为它是行尾):
在它之后,是友好的“抱歉...”消息。 看起来 ELMAH 确实更强大,但对于我的需求来说,这已经足够了。
By default, ASP.NET MVC applications have a view Shared/Error.aspx, inheriting from
If your controller uses the attribute [HandleError], all exceptions will continue to bubble until caught, and they will end up on that page.
I simply added an inline Page_Load (valid in this case since it's the end of the line):
After it, the friendly "Sorry..." message. It definitely looks like ELMAH is more robust, but for my needs, this was sufficient.