可能重复:
Maximum Request Length Exceeded Not Redirect on Error Page
当他上传超过最大尺寸的文件时,我尝试将用户重定向到某个错误页面。
我在下面的行中放入了Web.config,将文件限制为10MB:
<httpRuntime maxRequestLength="10240" executionTimeout="360" />
在我的页面上有一个简单的表格,带有标准的ASP文件上传控制和提交按钮。 我还在页面级别定义了重定向(我在Global.asax Application_Error处理中也尝试过,但结果是一样的):
protected void Page_Error(object sender, EventArgs e)
{
if (HttpContext.Current.Error is HttpException)
{
if ((HttpContext.Current.Error as HttpException).ErrorCode==-2147467259)
{
Server.ClearError();
Response.Redirect("~/Error.aspx");
}
}
}
我也试过Server.Transfer()
- 没有工作。
当我尝试上传大于10 MB的文件时,我可以调试并看到来自Page_Error
的代码完全执行了两次:即使使用Server.ClearError()
,但页面也没有重定向到{{ 1}}。相反,出现了标准的,丑陋的“连接已重置”错误页面。
如果错误是另一种类型,例如Error.aspx
上的0除以,则此代码可以正常工作。你能告诉我这里我做错了什么吗?
顺便说一句。我使用Visual Web Developer 2010 Express和.NET 4.0,WindowsXP。测试升级到VWD IIS服务器。
答案 0 :(得分:1)
您可能想尝试在web.config文件中定义通用错误页面,而不是通过代码处理它:
<customErrors defaultRedirect="/Path/to/myErrorPage.aspx" mode="On" />
您还可以为每个http状态代码定义特定页面,例如:
<customErrors defaultRedirect="/error/generic.aspx" mode="On">
<error statusCode="404" redirect="/error/filenotfound.aspx" />
<error statusCode="500" redirect="/error/server.html" />
</customErrors>
这是一篇不错的演示文章:http://support.microsoft.com/kb/306355
编辑:我发布了可疑的重复内容。在您的问题的评论中查看它。它看起来非常类似于你的。也许asp.net在处理这些类型的错误时遇到了问题。
编辑2:也 - 我相信您尝试处理的错误是http 413:
<error statusCode="413" redirect="/error/upload.aspx"/>
答案 1 :(得分:1)
<强>答案:强>
好的,所以我发现的答案如下:它不能通过“正常”异常处理来完成。 Folowing代码,在上面提到的链接中找到,放在global.asax中,是一些wokaround:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
System.Web.Configuration.HttpRuntimeSection runTime = (System.Web.Configuration.HttpRuntimeSection)System.Web.Configuration.WebConfigurationManager.GetSection("system.web/httpRuntime");
//Approx 100 Kb(for page content) size has been deducted because the maxRequestLength proprty is the page size, not only the file upload size
int maxRequestLength = (runTime.MaxRequestLength - 100) * 1024;
//This code is used to check the request length of the page and if the request length is greater than
//MaxRequestLength then retrun to the same page with extra query string value action=exception
HttpContext context = ((HttpApplication)sender).Context;
if (context.Request.ContentLength > maxRequestLength)
{
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
// Check if body contains data
if (workerRequest.HasEntityBody())
{
// get the total body length
int requestLength = workerRequest.GetTotalEntityBodyLength();
// Get the initial bytes loaded
int initialBytes = 0;
if (workerRequest.GetPreloadedEntityBody() != null)
initialBytes = workerRequest.GetPreloadedEntityBody().Length;
if (!workerRequest.IsEntireEntityBodyIsPreloaded())
{
byte[] buffer = new byte[512000];
// Set the received bytes to initial bytes before start reading
int receivedBytes = initialBytes;
while (requestLength - receivedBytes >= initialBytes)
{
// Read another set of bytes
initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
// Update the received bytes
receivedBytes += initialBytes;
}
initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
}
}
context.Server.ClearError(); //otherwise redirect will not work as expected
// Redirect the user
context.Response.Redirect("~/Error.aspx");
}
}