1

我試圖處理錯誤並顯示404,403等適當的頁面。ASP.NET MVC自定義錯誤處理,空白頁面

爲此,我製作了一個ErrorHandling過濾器,並按預期工作。也就是說,當我嘗試請求

mysite.com/Home/random_non-existent_action

用戶被重定向到我的自定義404頁,並返回404響應代碼。同樣適用於401和403。

我已經與web.config中做出錯誤處理過濾器和修改實現了這個:

enter image description here

這是問題開始的地方,我們的客戶要定製IIS的錯誤頁面爲好,例如,當我要求

mysite.com/images,正在顯示

IIS錯誤頁面,因爲目錄瀏覽被禁用,因爲這是防止在IIS的水平,我的錯誤處理過濾器不起作用。

要覆蓋IIS錯誤頁面,我心中已經添加以下代碼到web.config中

enter image description here

兩者的customErrors和httpErrors

現在出現在我的web.config, 當我請求

mysite.com/Home/random_non-existent_action

我的自定義404頁面顯示(因爲它被IIS旁路,我的錯誤處理過濾器工作)。但是,當我要求

mysite.com/content

mysite.com/images

我越來越有404個狀態碼空白響應。

當我改變responseMode重定向(而不是ExecuteURL),它的工作原理。但很顯然,我試圖用相對路徑導航到我的自定義錯誤頁面。

1-爲什麼當我嘗試覆蓋IIS自定義錯誤頁面時,我得到空白響應?

2,爲什麼當我提出responseMode =「重定向」,但它的工作不responseMode =「ExecuteURL」

任何幫助,因爲我已經搜查了許多類似計算器的話題,但沒有成功不勝感激。

我已經嘗試過所有用於existingResponse和responseMode值的組合。除了responseMode =「Redirect」之外的任何內容都會使其變爲空白並顯示相應的狀態碼。

編輯:

我到底所做的是,

enter image description here

寫HTML內容,並重定向到使用javascript我的自定義錯誤頁。

+0

確實爲這個問題幫你在所有的解決方案嗎? http://stackoverflow.com/questions/9997772/iis-7-5-custom-404-error-page-not-working-for-web-root-index-default –

+0

不,它不。目錄瀏覽已安裝 – OzanYukruk

回答

1

在MVC 5中,我遇到了同樣的問題。我解決了在Global.asax中處理這些錯誤的問題。將下面的代碼放在Global.asax.cs中。

public void Application_Error(Object sender, EventArgs e) 
    { 
     Exception exception = Server.GetLastError(); 
     Server.ClearError(); 

     var routeData = new RouteData(); 
     routeData.Values.Add("area", ""); 
     routeData.Values.Add("controller", "SystemAdministration"); 

     if (exception.GetType() == typeof(HttpException)) 
     { 
      var code = ((HttpException) exception).GetHttpCode(); 
      if (code == 404) 
      { 
       routeData.Values.Add("action", "NotFound"); 
      } 
      else if (code == 403 || code == 401) 
      { 
       routeData.Values.Add("action", "NoAuthorization"); 
      } 
      else 
      { 
       routeData.Values.Add("action", "Index"); 
       routeData.Values.Add("statusCode", code); 
      } 
     } 
     else 
     { 
      routeData.Values.Add("action", "Index"); 
      routeData.Values.Add("statusCode", 500); 
     } 
     routeData.Values.Add("exception", exception); 

     Response.TrySkipIisCustomErrors = true; 
     IController controller = new ErrorController(); 
     controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData)); 
     Response.End(); 
    } 

希望這能對你的作品...

+0

Response.End();和Response.TrySkipIisCustomErrors = true;在行動方法是我的情況的關鍵。 –