2016-08-23 104 views
3

我得到錯誤:MVC的Application_Start本地主機重定向你太多次

localhost redirected you too many times.

當我重定向到從 Application_Start方法錯誤頁面

我的代碼看起來是這樣的:

protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
    } 

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

     if (exception != null) 
     {  
      Session["w"] = exception; 
      Response.Clear(); 
      Server.ClearError(); 
      Response.Redirect("~/Admin/Error");  
     } 
    } 
} 
+0

當您顯示視圖/管理員/錯誤時,您是否有錯誤? – kblau

+0

沒有...它給了我一個空的頁面說localhost重定向你太多次 – Tom

回答

0

這不是在這種情況下,使用Session是一個好主意。如果錯誤是由IHttpHandler(未標記爲IRequiresSessionState)觸發的,則訪問會話將失敗。所以,你會有一個重定向循環。

刪除使用會話,並嘗試使用:

Response.Redirect(String.Format("~/Admin/Error?w={0}", exception.Message)); 
+0

謝謝你的幫助:)現在它給了我這個消息路徑'/favicon.ico'的控制器未找到或做不實施IController。 我想我需要第一個過濾異常類型 – Tom

+0

當然,我沒有:)現在我有另一個問題...當它重定向到錯誤視圖,如果用戶點擊刷新它再次使相同的錯誤,可以你請幫我避免那 – Tom

+0

謝謝@湯姆。所以,正如我所說如果你有一個新的問題,請點擊[Ask Question](問問題)(http://stackoverflow.com/questions/ask)按鈕。如果有助於提供上下文,請包含此問題的鏈接。在新問題中描述你的新問題,並把鏈接留在這裏 – Marusyk

0

的問題是更多的則可能與造成您已上述缺失favicon.ico文件File Not Found錯誤。

要解決,只需將favicon.ico文件添加到項目根目錄中即可。

(可選)您可以更新錯誤處理以避免在檢測到404時重定向。

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

    if (exception != null && exception != 404) 
    {  
     Session["w"] = exception; 
     Response.Clear(); 
     Server.ClearError(); 
     Response.Redirect("~/Admin/Error");  
    } 
} 
相關問題