【发布时间】:2011-04-10 22:17:23
【问题描述】:
如何在 mvc 中捕获渲染异常? (我使用的是第一个版本)
我从值得的案例场景开始 - .ascx 文件中的编译时错误,这是我发现的:
1) 在调试模式下,Visual Studio 说 Controller.ExecuteCore(), InvokeAction 行中存在未处理的异常:
try {
string actionName = RouteData.GetRequiredString("action");
if (!ActionInvoker.InvokeAction(ControllerContext, actionName)) { // this line
HandleUnknownAction(actionName);
}
}
finally {
TempData.Save(ControllerContext, TempDataProvider);
}
2) Application.OnError 没有被调用
3) Controller.OnError 没有被调用
4) 当我在这里注入 catch() 时,它也没有被触发。
5) try.. 在 masterpage 中的 catch 没有被触发(尽管它存在于 stacktrace 中!)
6) try.. 页面中的捕获未触发(它也存在于堆栈跟踪中!)
当我包装特定的 RenderPartial 方法时,我能够捕获异常仅,如下所示:
<% try
{ %>
<%Html.RenderPartial("Search/" + Model.SearchCategory + "SearchList", Model); %>
<%}
catch (Exception ex)
{ %><% HttpContext.Current.Response.Redirect("/en/App/Error"); %><% } %>
我还能做什么?
- 我无法返回 RedirectAction 因为我们不在控制器中
- 我试试 HttpContext.Current.Response.Redirect 但它并没有真正起作用(我是 有文本的页面:对象已移动 到 .)
【问题讨论】:
标签: asp.net-mvc error-handling