2010-07-01 167 views
0

我遇到了一個異常問題,顯示頁面中的應用程序堆棧跟蹤。ASP.NET MVC異常沒有正確路由到Customerrors默認路由

我認爲我已經加入這個到web.config減輕這樣的:

<customErrors mode="On" defaultRedirect="~/error/GenericError"> 
    <error statusCode="403" redirect="~/error/NoAccess" /> 
    <error statusCode="404" redirect="~/error/NotFound" /> 
</customErrors> 

它爲不存在的路線,而不是當一個控制器拋出異常。這裏的控制器邏輯:

[HandleError] 
public class DebugController : Controller 
{  
    public ActionResult Index() 
    { 
     throw new Exception("** Testing custom error Handling **"); 
     return View(); 
    } 
} 



public class ErrorController : Controller 
{ 
    // 
    // GET: /Error/ 

    public ActionResult NotFound() 
    { 
     ViewData["error"] = "That page does not exist."; 
     return View(); 
    } 

    public ActionResult GenericError() 
    { 
     if (null == TempData["error"])) 
     { 
      ViewData["error"] = "We're sorry, but an error has occurred."; 
     } 
     else 
     { 
      ViewData["error"] = TempData["error"]; 
     } 

     return View(); 
    } 

    public ActionResult NoAccess() 
    { 
     ViewData["error"] = "You are not authorized to access application"; 
     return View(); 
    } 

} 

而這裏的景觀:

<%@ Page Title="" Language="C#" 
    MasterPageFile="~/Views/Shared/CenterContentNoSidebar.Master" 
    Inherits="System.Web.Mvc.ViewPage<System.Web.Mvc.HandleErrorInfo>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
GenericError 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <h2>An Error has occurred</h2> 
    <br /> 
    <%= ViewData["error"] %> 
</asp:Content> 

<asp:Content ID="Content3" ContentPlaceHolderID="NavContent" runat="server"> 
</asp:Content> 

我必須把在Global.asax的東西,使這項工作?

回答

2

所以的HandleError屬性被實際處理的錯誤,並試圖從控制器查看的文件夾或共享位置返回一個Error.aspx圖。 customErrors web.config部分永遠不會被擊中,並且由於頁面可能不存在,反正會顯示黃色的死亡屏幕。不存在的路由工作原因IIS正在拋出404錯誤,並且沒有控制器或HandleError屬性的上下文。如果你想自己處理錯誤,我會建議刪除HandleError屬性,並讓customErrors通過一些。從你的錯誤控制器,儘管你將不得不抓住最後一個異常,並做了一些事情。這裏有一個很好的參考資料,說明我在談論什麼。

http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html

這是怎樣的一個一個或其他類型的事情,的HandleError屬性或web.config中的customErrors節。

1

您看到此行爲是因爲您放在最前面的[HandleError]。有關[HandleError]屬性的更多信息,請參閱Stack上的此查詢/答案。

StackOverflow Handle Error Q&A