防止应用程序事件日志中出现 Asp.net MVC2 异常
自从升级到 ASP.NET MVC2 以来,我们注意到 Windows 应用程序事件日志充满了如下条目:
Event code: 3005
Event message: An unhandled exception has occurred.
Event time: 11/4/2010 3:29:16 PM
Event time (UTC): 11/4/2010 7:29:16 PM
Event ID: 2039870297302976340236
Event sequence: 6422
Event occurrence: 864
Event detail code: 0
...
Exception information:
Exception type: ArgumentException
Exception message: The parameters dictionary contains a null entry for parameter 'someParam' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(System.String, Int32)' in 'SomeNameSpace.SomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
请求错误,因此我们收到此错误。很好,但我想在我的应用程序中处理这个问题,而不是让它冒泡出事件日志。有什么建议吗?
编辑
Since upgrading to ASP.NET MVC2, we've noticed the Windows application event log is full of entries as follows:
Event code: 3005
Event message: An unhandled exception has occurred.
Event time: 11/4/2010 3:29:16 PM
Event time (UTC): 11/4/2010 7:29:16 PM
Event ID: 2039870297302976340236
Event sequence: 6422
Event occurrence: 864
Event detail code: 0
...
Exception information:
Exception type: ArgumentException
Exception message: The parameters dictionary contains a null entry for parameter 'someParam' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(System.String, Int32)' in 'SomeNameSpace.SomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
The request is bad, so we're getting this error. Great, but I'd like to handle this in my application rather than allowing it to bubble out the event log. Any suggestions?
EDIT
Look at the answer to this question, would it be more sensible to set up Route Constraints to deal with this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是 ASP.NET MVC 特有的行为,而是任何 ASP.NET 应用程序的正常行为。所有未处理的异常都会被记录,这看起来很正常。所以你需要处理它们。对于这种类型的异常,您确实可以使用路由约束并自定义 404 和 500 错误页面。
Global.asax 中还有
Application_Error
处理程序,可用于处理异常。对于控制器级别的异常,有[HandleError]< /code>
动作过滤器。
This is not something specific to ASP.NET MVC but it is the normal behavior of any ASP.NET application. All unhanded exceptions are logged which seems normal. So you need to handle them. For this type of exception you could indeed use route constraints and have custom 404 and 500 error pages.
There's also the
Application_Error
handler in Global.asax which could be used to handle exceptions. For controller level exceptions there's the[HandleError]
action filter.