【发布时间】:2016-06-27 09:13:58
【问题描述】:
我的 web.config 设置如下:
<httpRuntime maxRequestLength="5000" executionTimeout="120"/>
我在 Global.asax 中的错误处理代码:
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
//Exception ex;
string sourcepath = System.IO.Path.GetFileName(Request.Path);
if (string.Compare(System.IO.Path.GetFileName(Request.Path), "vendorMassUpload.aspx", StringComparison.OrdinalIgnoreCase) == 0)
{
System.Exception lastException = Server.GetLastError();
HttpException httpException = (HttpException)lastException;
int httpCode = httpException.GetHttpCode();
int errorCode = httpException.ErrorCode;
if (errorCode == -2147467259)
{
Server.ClearError();
Response.Redirect("~/vendorManagement/vendorMassUpload.aspx?fileTooLarge=true");
}
}
}
我在测试中用于上传的文件大小为 4999 KB。
它将到达Response.Redirect,但它只会在 Internet Explorer 中显示This page can’t be displayed。
如果我在ButtonUpload_Click 中的throw new Exception("testing exception"); 和if (errorCode == -2147467259) 发表评论,一切正常。
出了什么问题?
我要做的就是重定向用户并在他们上传大文件时向他们显示自定义消息,而不是告诉他们This page can’t be displayed。
【问题讨论】:
-
默认情况下,如果输入文件大小大于
maxrequestlength,ASP .NET 会终止处理请求而不抛出异常。此解决方案可能会有所帮助:stackoverflow.com/questions/2756448/… -
@Tetsuya Yamamoto 感谢您的链接,我发现我的代码在 Visual Studio 开发 Web 服务器中不起作用,但它在本地 IIS 中起作用。但是为什么?