2010-01-28 3 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

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가 있습니다. Global.asax 파일에 응용 프로그램 오류를 처리 할 수 ​​

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로 설정해야합니다. 전의.

0

이러한 제한은 실제로 사이트에서 DoS을 수행하는 것을 방지하기 위해 web.config에서 제한됩니다. 사용자에게 파일 크기 제약 조건에 대한 시각적 단서를 제공하고 표준 오류 처리기가 대신 처리하도록 최선을 다할 수 있습니다.

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

그것은 사용자에게 오류 정보를 제공하는 것은 좋은 생각이 아니다; 대신 관리자 용으로 예약해야합니다. 파일 크기 오류는 오류 뿐이며 유효성 검사와 관련이 없으며 사용자에게 피드백을 제공해야합니다.

1

, 당신은이 목적을 위해 설계 처리기를 사용하는 것이 좋습니다 :

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> 
관련 문제