HandleError 的问题
我有以下操作方法:
[HandleFtmsError]
public ActionResult PerformanceChart(ChartViewModel chart)
{
var x = 1;
var y = 0;
var z = x/y;
return Json("");
}
其中 HaneleFtmsError
定义为:
public class HandleFtmsErrorAttribute : System.Web.Mvc.HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
base.OnException(context);
if (context.ExceptionHandled)
RaiseErrorSignal(context.Exception);
}
private static void RaiseErrorSignal(Exception e)
{
var context = HttpContext.Current;
ErrorSignal.FromContext(context).Raise(e, context);
}
}
我认为操作方法上的属性会在 DivideByZero 异常的情况下执行,但它不起作用。我所看到的只是我进行除法的行上的代码中断。我做错了什么吗?
I have the following Action Method:
[HandleFtmsError]
public ActionResult PerformanceChart(ChartViewModel chart)
{
var x = 1;
var y = 0;
var z = x/y;
return Json("");
}
where HaneleFtmsError
is defined as:
public class HandleFtmsErrorAttribute : System.Web.Mvc.HandleErrorAttribute
{
public override void OnException(ExceptionContext context)
{
base.OnException(context);
if (context.ExceptionHandled)
RaiseErrorSignal(context.Exception);
}
private static void RaiseErrorSignal(Exception e)
{
var context = HttpContext.Current;
ErrorSignal.FromContext(context).Raise(e, context);
}
}
I thought that attribute over the action method would have been executed with a DivideByZero exception, but it's not working. All I'm seeing is the code breaks on the line where I'm doing the division. Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您说“代码中断”时,您的意思是它正在侵入调试器吗?这可能只是标准调试器行为,您可以通过“调试”菜单的“异常...”项进行更改。如果您再次按 F5 - 或在没有调试的情况下运行 - 您可能会看到您期望的行为。
MVC 不会阻止抛出异常(这正是调试器所寻找的)——它只是通过注意控制器上的属性并适当地传递信息来处理异常。当调试器介入时,它还没有机会这样做。
When you say "the code breaks" do you mean it's breaking into the debugger? That's probably just the standard debugger behaviour, which you can change via the Debug menu's "Exceptions..." item. If you hit F5 again - or run without debugging - you may see the behaviour you expect.
MVC isn't preventing the exception from being thrown (which is what the debugger's looking for) - it's just handling the exception by noticing the attribute on the controller and passing the information on appropriately. At the point where the debugger's breaking in, there hasn't been a chance for it to do that yet.