2011-07-21 42 views
1

我跟着這些鏈接:超過最大請求長度不上的錯誤頁面重定向

  1. Catching "Maximum request length exceeded"
  2. ASP.NET - how to show a error page when uploading big file (Maximum request length exceeded)?

web.config

顯示錯誤頁面來處理上傳文件超過 maxRequestLength

但我的問題是,它沒有重定向到錯誤p年齡(該消息表示該網頁無法顯示)。我不知道我錯過了什麼。

這裏是我的代碼@Global.asax

void Application_Error(object sender, EventArgs e) 
{  
    if (IsMaxRequestLengthExceeded(Server.GetLastError())) 
    { 
     this.Server.ClearError(); 
     this.Server.Transfer("~/Error.html"); 
    } 
} 

private bool IsMaxRequestLengthExceeded(Exception ex) 
{ 
    Exception main; 
    HttpUnhandledException unhandledEx = (HttpUnhandledException)ex; 

    if (unhandledEx != null && unhandledEx.ErrorCode == -2147467259) 
    { 
     main = unhandledEx.InnerException; 
    } 
    else 
    { 
     main = unhandledEx; 
    } 

    HttpException httpException = (HttpException)main; 
    if (httpException != null && httpException.ErrorCode == -2147467259) 
    { 
     if (httpException.StackTrace.Contains("GetEntireRawContent")) 
     { 
      return true; 
     } 
    } 

    return false; 
} 

而@web.config

<httpRuntime executionTimeout="1200" /> 
<customErrors defaultRedirect="Error.html" mode="On"> 
</customErrors> 

據發現,當maxRequestLength沒有初始化,則默認設置爲4MB。 (我沒有設置它,因爲它對我來說並不重要。)

希望你能幫助我解決這個問題。謝謝

+0

不應該 「的error.html」 在web.config中與Global.asax中的「〜/ Error.html」相同? –

+0

嗨,使用'Error.html'導致錯誤,我需要'〜'字符來指定虛擬目錄的根路徑。 – KaeL

+0

不應該在兩個地方都是「〜/ Error.html」(web.config也是)? –

回答

0

您可以添加

<httpErrors errorMode="Custom" existingResponse="Replace"> 
    <remove statusCode="404" subStatusCode="13" /> 
    <error statusCode="404" subStatusCode="13" prefixLanguageFilePath="" path="http://localhost:1899/ErrorUpload.aspx" responseMode="Redirect" /> 
</httpErrors> 

剛過

<security> 
    <requestFiltering> 
    <requestLimits maxAllowedContentLength="5000000" /> 
    </requestFiltering> 
</security> 

,你重定向到一個錯誤頁面...

相關問題