捕获控制器中的异常

时间:2014-01-20 17:42:55

标签: asp.net-mvc exception razorengine

看起来如果在控制器内部发生异常,即使在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);
}

1 个答案:

答案 0 :(得分:1)

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