在ASP.NET MVC2正确的错误处理 [英] Proper error handling in ASP.NET MVC2

查看:98
本文介绍了在ASP.NET MVC2正确的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个onException的覆盖(ExceptionContext filterContext)在我的基本控制器中的任何错误赶应用程序,然后再登录他们。我得到我的应用程序的问题是这种特殊的方法发射了四次某些错误。我将带您完成一个场景:

I have an override OnException(ExceptionContext filterContext) in my base controller to catch the app during any errors, and then log them. The problem I'm getting in my app is this particular method is fired off four times for certain errors. I'll walk you through a scenario:

比方说,我浏览到:
的http://本地主机:180 / someController / someAction someId = XX

和我可怜的对象在我的code处理。在传递的ID是无效的,并获取了一些空的对象,我的话,我的坏对象处理BC,尽量空对象进行操作。我得到一个异常。

And I have poor object handling in my code. The Id passed in is an invalid one, and it retrieves some null object, I then, bc of my bad object handling, try to operate on a null object. I get an exception.

BaseController的onException的在这里发了。

BaseController's OnException is fired off here.

这是空对象还是返回到视图,其中视图试图将其绑定到的东西,或者你有什么。

That null object is still returned out to the view, where the view tries to bind it to something, or what have you.

BaseController的onException的是在这里再次发射了,在视图中的错误。

BaseController's OnException is fired off here again, for the error in the view.

从本质上讲,只有一个错误对我很重要,但涓流效应导致更多的错误来断火,和垃圾邮件我的收件箱: - /

Essentially, only one error is important to me, but the trickle up effect is causing more errors to fire off, and spam my inbox :-/.

什么是捕捉MVC2错误,并且不会有这种事发生在我的正确方法?

What is the correct way to catch an error in MVC2 and not have this happen to me?

推荐答案

我建议你从的HandleError属性继承,并在那里滚动你的异常处理。重写onException的一个控制器上意味着你要么有大量的异常处理code在很多控制器或从基地之一,这是由于MVC管道是不是真的有必要在任何情况下,你继承。

I would recommend you inheriting from the HandleError attribute and rolling your exception handling in there. Overriding the OnException on a single controller means you either have a lot of exception handling code in a lot of controllers or your inherit from a base one, which due to the MVC pipeline is not really necessary in either case.

使用属性,你应该有每执行操作时出现错误之一发生,一旦出错处理也不会再次触发。希望这将减少重复异常消息。

By using the attribute, you should have one occurrence of an error per action executed, and once the error is handled it won't fire again. Hopefully this will cut down on repeat exception messages.

我个人使用属性的异常处理的原因,而是更清洁和可重复使用的,并得到的摆脱大量的噪音的我的行动中。

I personally use attributes for the exception handling cause it's cleaner and more reusable and get's rid of a lot of noise within my actions.

这篇关于在ASP.NET MVC2正确的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆