2011-09-17 32 views
1

在請求長度超過maxQueryStringLength中指定的情況下,如何更換標準錯誤頁面並向用戶顯示更友好的內容?如果發生maxQueryStringLength異常,如何重定向到自定義頁面?

注意:雖然作爲一個HttpException這屬於通用400錯誤,我想分開QueryLength條件並顯示一個非常具體的頁面,爲這個特定的錯誤。所以,我不能使用「customErrors」部分來指示頁面,而是需要以編程方式進行過濾。下面的問題是不起作用的。

protected virtual void Application_Error(Object sender, EventArgs e) 
    { 
     Exception ex = Server.GetLastError(); 
     if (logs != null) logs.ExceptionMsg("Application exception", this, ex); 

     var httpex = ex as System.Web.HttpException; 
     if (httpex != null && httpex.ErrorCode == -2147467259) 
     { 
      Server.ClearError(); 
      Server.TransferRequest(@"~/main/maxlengthexceeded", false); 
     } 
    } 

問題是Server.TransferRequest不起作用。有什麼替代方法可以告訴ASP.NET要加載哪個頁面?

回答

1

,如果你能抓住你所得到的錯誤類型/個數,那麼你可以只爲配置不同的錯誤/重定向頁面,在這裏的web.config中的配置的例子:

<configuration> 
    <system.web> 
    <customErrors defaultRedirect="GenericError.htm" 
        mode="RemoteOnly"> 
     <error statusCode="500" 
      redirect="InternalError.htm"/> 
    </customErrors> 
    </system.web> 
</configuration> 

你可以在這裏看到完整的文章:Displaying a Custom Error Page

相關問題