2011-12-19 65 views
1

我爲我的應用程序定義了自定義錯誤處理,它的所有工作都非常出色 - 當找不到資源時,正確的「NotFound」視圖呈現。當發生不受歡迎的異常時,'ServerError'視圖呈現。MVC3錯誤處理 - 堅持使用稱爲'錯誤'的視圖

我面對的問題是,我的應用程序堅持嘗試找到一個名爲'錯誤'的視圖,但沒有找到它,因爲我沒有一個,因此這個異常在我的自定義錯誤處理例程期間被拋出:

「視圖‘錯誤’或它的主人沒有被發現或沒有視圖引擎支持的搜索位置以下地點進行了全面搜查。......」

我有一個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); 
    } 
} 

我已接通的customErrors「開啓」在我的web.config:

<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" } 
    ); 

我有一個BaseController其中包含OnActionExecuted程序:

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); 
} 

是否有其他地方的框架定義了視圖來呈現未處理的異常事件?

我的自定義錯誤處理例程錯過了最後一步,這將確保框架不再期望找到'錯誤'視圖?

回答

0

我已經刪除自動添加的過濾器將所有控制器...由Eranga的建議行 - 所以這不是什麼導致框架來搜索「錯誤」視圖。

我遇到的問題是由位於我的控制器之一上的[HandleError]屬性標記遺留下來的。

所以有意思的是:儘管我的控制器在其類定義的頂部裝飾了[HandleError]屬性,但是我的自定義錯誤處理例程仍然在web.config中定義稱爲 - 並正確呈現所需的視圖...

框架錯誤處理(由HandleErrorAttribute定義)將失敗,我的Application_Error將捕獲異常並通過我的'記錄器'實例將其默認記錄到數據庫。 ..然後我的自定義錯誤處理例程將成功完成。 E-mail:

3

您需要刪除Global.asax.cs文件中的HandleErrorAttribute處理程序。該屬性將視圖設置爲Error。然後MVC運行時不會處理異常,異常將傳播到Asp.Net運行時,它將使用customErrors部分來顯示頁面。

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

Eranga:這不是問題,但讓我找到如下所述的解決方案 - 謝謝! – JTech 2011-12-19 04:15:02

+1

謝謝!那對我來說就是這樣。對於新的模板中的一些新用戶,這將位於App_Start文件夾FilterConfig.cs中。 – 2012-07-26 18:03:51