2012-07-05 150 views
8

IIS配置我有一個簡單ASP.NET MVC 3網站託管IIS 7.0和上午顯示自定義HTTP錯誤頁的404.13 HTTP狀態代碼有困難。爲404.13自定義錯誤

我有以下的配置,我Web.Config中

<system.web> 
    <httpRuntime maxRequestLength="2048"/> 
    <customErrors mode="Off"/> 
</system.web> 

<system.webServer> 
    <httpErrors errorMode="Custom" existingResponse="Replace"> 
     <clear/> 
     <error statusCode="404" subStatusCode="-1" path="/home/showerror" responseMode="ExecuteURL" /> 
     <error statusCode="404" subStatusCode="13" path="/home/showerror" responseMode="ExecuteURL" /> 
    </httpErrors> 
    <security> 
     <requestFiltering> 
      <requestLimits maxAllowedContentLength="1048576"/> 
     </requestFiltering> 
    </security> 
</system.webServer> 

當我瀏覽到一個不存在我的錯誤頁面正常顯示的網頁。但是,如果我上傳大於1MB的文件,則會顯示一個空的404響應。網址永遠不會執行。如果我將響應模式更改爲重定向然後用戶被正確重定向。

+0

的可能重複的[顯示自定義錯誤頁,當文件上傳超出允許大小在ASP.NET MVC(http://stackoverflow.com/questions/2759193/display-custom-error-page-when-file -upload-exceed-allowed-size-in-asp-net-mvc) –

+1

@DarinDimitrov該問題使用responseMode = Redirect,這是一個不同的問題,它不是重複的。 –

+1

它使用重定向,因爲ExecuteURL不適用於404.13,如重複答案中所述。 –

回答

3

由於配置系統的鎖定功能,最不可能顯示自定義錯誤。試試下面的命令來解鎖:

%windir%\System32\inetsrv\appcmd unlock config -section:system.webserver/httperrors 

一對夫婦與RequestFilters和CustomError頁玩弄天之後,它終於爲我工作。

1

我在集成模式下遇到了與IIS 7.5相同的問題。

最終我放棄了試圖處理在web.config方法錯誤和移動的錯誤檢測和處理的Application_EndRequest代替:

以下內容添加到您的Global.asax文件:

如果您沒有global.asax(MVC),那麼您可以將其添加爲頁面錯誤。

Protected Sub Application_EndRequest(ByVal sender As Object, ByVal e As System.EventArgs) 

    Dim context As HttpContext = HttpContext.Current.ApplicationInstance.Context 
    If Not IsNothing(context) Then 

     If context.Response.StatusCode = 404 And 
      context.Response.SubStatusCode = 13 Then 

       context.Response.ClearHeaders() 
       context.Server.Transfer("~/errors/404.13.aspx", False) 
     End If 

    End If 
End Sub 
+0

該解決方案在MVC中不適用於我。它拋出「執行子請求錯誤...」異常。如果我嘗試使用Response.Redirect,它會拋出「超出最大請求長度」。例外。 –