2010-01-28 92 views
0
protected void Application_BeginRequest(object sender, EventArgs e) 
    { 


     const int maxFileSizeKBytes = 10240; //10 MB 
     const int maxRequestSizeKBytes = 305200; //~298 MB 

     if (Request.ContentLength > (maxRequestSizeKBytes * 1024)) 
     { 
      Response.Redirect(".aspx?requestSize=" + Request.ContentLength.ToString()); 
     } 


     for (int i = 0; i < Request.Files.Count; i++) 
     { 
      if (Request.Files[i].ContentLength > (maxFileSizeKBytes * 1024)) 
      { 
       Response.Redirect(".aspx?fileSize=" + Request.Files[i].ContentLength.ToString()); 
      } 
     } 

    } 

此代碼位於Global.asax.cs頁面中。 我需要重定向到觸發此檢查的頁面。我需要知道ticketId或projectId參數。例如,我在查看項目頁面/Project/ViewProject.aspx?projectId=1上創建新票證,我需要用一條有意義的消息重定向到此頁面,因爲我認爲重定向到另一個頁面以顯示錯誤消息不是一個好主意。重定向應用程序級錯誤處理程序

回答

1

爲什麼不把這些檢查放在基本頁面類的Load處理程序中,以使ViewProject(以及其他需要檢查的東西)派生自?然後,如果檢查失敗,則可以使錯誤標籤可見。未經測試的代碼:

public class BasePage : Page{ 
    protected virtual Label ErrorLabel { get; set; }; 
    protected override OnLoad(object sender, EventArgs e) { 
    base.OnLoad(sender, e); 

    const int maxFileSizeKBytes = 10240; //10 MB 
    const int maxRequestSizeKBytes = 305200; //~298 MB 

    if (Request.ContentLength > (maxRequestSizeKBytes * 1024)) 
    { 
     ErrorLabel.Text = "Request length "+Request.ContentLength+" was too long." 
     ErrorLabel.Visible = true; 
    } 


    for (int i = 0; i < Request.Files.Count; i++) 
    { 
     if (Request.Files[i].ContentLength > (maxFileSizeKBytes * 1024)) 
     { 
      ErrorLabel.Text = "File length "+ Request.Files[i].ContentLength +" was too long." 
      ErrorLabel.Visible = true; 
     } 
    } 
    } 
} 

public class ViewProject : BasePage { 
    protected override Label ErrorLabel { 
    get { return LocalErrorLabel; } // something defined in HTML template 
    set { throw new NotSupportedException(); } 
    } 
} 

這樣你留在同一頁上,你已經有ticketId和projectId。

0

你可以用Server.Transfer嘗試這樣的事情。該URL將保持不變。做一個Response.Redirect會再次發送一個302到同一個頁面(有時候可能發送給你一個無限循環,例如在頁面加載mypage.aspx時嘗試使用Response.Redirect(mypage.aspx))。

string errorPage = "~//Error.aspx"; 
Server.Transfer(errorPage, false);  
HttpContext.Current.Server.ClearError(); 
HttpContext.Current.Response.ClearContent(); 

無論哪種情況,您都應該將第二個參數設置爲false以避免線程中止異常。恩。

Response.Redirect("mypage.aspx",false); 
Server.Transfer("myerror.aspx",false); 
0

這些限制實際上受限於web.config中,作爲防止人們在您的站點上執行DoS的最佳做法。您最好爲用戶提供關於文件大小限制的可視提示,然後讓標準錯誤處理程序接管。

http://msdn.microsoft.com/en-us/library/e1f13641.aspx

這不是一個好主意,讓錯誤的詳細信息給用戶;你應該保留這個管理員。文件大小錯誤就是......錯誤,與驗證無關,這就是你應該向用戶提供反饋的內容。

1

到Global.asax文件處理應用程序錯誤,你應該考慮使用爲此目的而設計的處理器:

protected void Application_Error(object sender, EventArgs e) 
{ 
    //get exception causing event 
    Exception lastException = Server.GetLastError().GetBaseException(); 

    //log exception, redirect based on exception that occurred, etc. 
} 

你的「設置」,如maxRequestSizeKBytes應在web.config中使用來定義MaxRequestLength property

實施例:

<system.web> 
    <httpRuntime maxRequestLength="305200" executionTimeout="120" /> 
</system.web>