【问题标题】:MVC3 Error Handling - Insists on using View called 'Error'MVC3 错误处理 - 坚持使用名为“错误”的视图
【发布时间】:2012-01-23 06:19:14
【问题描述】:

我为我的应用程序定义了自定义错误处理,并且一切都非常好 - 当找不到资源时,会呈现正确的“未找到”视图。当发生未处理的异常时,将呈现“ServerError”视图。

我面临的问题是我的应用程序坚持试图找到一个名为“错误”的视图,但没有找到它,因为我没有,因此在我的自定义错误处理例程中抛出了这个异常:

“未找到视图'错误'或其主视图或没有视图引擎支持搜索的位置。搜索了以下位置:...”

我有一个 Application_Error 事件处理程序,它记录所有未处理的异常:

protected void Application_Error(Object sender, EventArgs e)
{
    Exception lastEx = Server.GetLastError();

    // Attempt to Log the error
    try
    {
        Utils.Logger.Error(lastEx);
    }
    catch (Exception loggingEx)
    {
        // Try and write the original ex to the Trace
        Utils.TryTrace(lastEx.Message);

        // Try and write the logging exception to the Trace
        Utils.TryTrace(loggingEx.Message);
    }
}

我在 web.config 中将 customErrors 设为“开启”:

<customErrors mode="On" defaultRedirect="blah">
    <error statusCode="404" redirect="dee"/>
    <error statusCode="500" redirect="blah"/>
</customErrors>

我在 Global.asax.cs RegisterRoutes 方法中定义了路由,这些路由对应于上面 web.config 中定义的重定向:

routes.MapRoute(
    "Error - 404",
    "dee",
    new { controller = "Error", action = "NotFound" }
    );

routes.MapRoute(
    "ServerError", // When this route is matched we want minimal error info displayed
    "blah",
    new { controller = "Error", action = "ServerError" }
    );

我有一个包含 OnActionExecuted 例程的 BaseController:

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    Logger.Info(String.Format(Constants.LOG_ACTION_EXECUTED, filterContext.Controller.ToString(), filterContext.ActionDescriptor.ActionName));
    // Log any exceptions
    if (filterContext.Exception != null)
    {
        Stack<string> messages = new Stack<string>();
        Exception current = filterContext.Exception;
        messages.Push(current.Message);
        while (current.InnerException != null)
        {
            messages.Push(current.InnerException.Message);                    
            current = current.InnerException;
        }
        StringBuilder result = new StringBuilder();
        while (messages.Count != 0)
        {
            result.Append(messages.Pop());
            string nextline = messages.Count > 0 ? "OUTER EXCEPTION " + messages.Count + ": " : "";
            result.Append(Environment.NewLine);
        }
        Logger.Error(result.ToString());
    }
    base.OnActionExecuted(filterContext);
}

框架是否在其他地方定义了在发生未处理的异常时要呈现哪个视图?

我的自定义错误处理例程是否缺少最后一步,以确保框架不再期望找到“错误”视图???

【问题讨论】:

    标签: asp.net-mvc-3 error-handling


    【解决方案1】:

    您需要删除Global.asax.cs 文件中的HandleErrorAttribute 处理程序。此属性将视图设置为Error。然后 MVC 运行时将不处理异常,异常将传播到 Asp.Net 运行时,它将使用 customErrors 部分显示页面。

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute()); // remove this line
        }
    

    【讨论】:

    • Eranga:这不是问题,但让我找到了如下所述的解决方案 - 谢谢!
    • 谢谢!对我来说就是这样。对于一些新手,在较新的模板中,这将位于 App_Start 文件夹 FilterConfig.cs 中。
    【解决方案2】:

    我已经删除了将过滤器自动添加到所有控制器的行...正如 Eranga 所建议的那样 - 所以这不是导致框架搜索“错误”视图的原因。

    我遇到的问题是由位于我的一个控制器顶部的一些剩余 [HandleError] 属性标签引起的。

    所以有趣的是:尽管我的控制器在其类定义的顶部装饰了 [HandleError] 属性,但我在 web.config 中定义的自定义错误处理例程仍然被调用- 并且正在正确呈现所需的视图...

    框架错误处理(由 HandleErrorAttribute 定义)将失败,我的 Application_Error 将捕获异常并通过我的“记录器”实例将其静默记录到数据库...然后我的自定义错误处理例程将成功完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 2016-11-11
      相关资源
      最近更新 更多