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