捕获控制器中的异常(Catching Exceptions in the Controller)

编程入门 行业动态 更新时间:2024-10-25 21:21:08
捕获控制器中的异常(Catching Exceptions in the Controller)

看起来如果在控制器内发生异常,即使在try-catch块中捕获“异常”,视图引擎也不会进入预期的视图?

或者我在这里遗漏了一些东西:

public ActionResult MyController(int? param1, DateTime? param2) { MyModel model = new MyModel(); try { model = new MyModel(); //AN ERROR HAPPENS HERE (so the code goes to catch the Exception): model.Connection.Initialize(); } catch (Exception ex) { ViewBag.ErrorMessage = ex.Message; } //when I put a break point I get to this following line, however, "MyView" is never displayed!? return PartialView("MyView", model); }

It seems like if an Exception occurs inside a controller, the view-engine won't go to the intended view, even if the "exception" is caught inside a try-catch block?

or I'm missing something here:

public ActionResult MyController(int? param1, DateTime? param2) { MyModel model = new MyModel(); try { model = new MyModel(); //AN ERROR HAPPENS HERE (so the code goes to catch the Exception): model.Connection.Initialize(); } catch (Exception ex) { ViewBag.ErrorMessage = ex.Message; } //when I put a break point I get to this following line, however, "MyView" is never displayed!? return PartialView("MyView", model); }

最满意答案

您可能会在视图中的某个位置获得另一个例外。 查看浏览器页面上显示的堆栈跟踪并修复它。

You might be getting another exception afterwards, somewhere in your view. Look at the stack trace that displays on the browser page and fix that.

更多推荐

本文发布于:2023-08-02 10:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1375425.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器中   异常   Catching   Exceptions   Controller

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!