3 回答
TA贡献1934条经验 获得超2个赞
不幸的是,没有简单的方法来捕获这样的例外。我所做的是覆盖页面级别的OnError方法或global.asax中的Application_Error,然后检查它是否是最大请求失败,如果是,则转移到错误页面。
protected override void OnError(EventArgs e) .....
private void Application_Error(object sender, EventArgs e)
{
if (GlobalHelper.IsMaxRequestExceededException(this.Server.GetLastError()))
{
this.Server.ClearError();
this.Server.Transfer("~/error/UploadTooLarge.aspx");
}
}
这是一个黑客,但下面的代码适合我
const int TimedOutExceptionCode = -2147467259;
public static bool IsMaxRequestExceededException(Exception e)
{
// unhandled errors = caught at global.ascx level
// http exception = caught at page level
Exception main;
var unhandled = e as HttpUnhandledException;
if (unhandled != null && unhandled.ErrorCode == TimedOutExceptionCode)
{
main = unhandled.InnerException;
}
else
{
main = e;
}
var http = main as HttpException;
if (http != null && http.ErrorCode == TimedOutExceptionCode)
{
// hack: no real method of identifying if the error is max request exceeded as
// it is treated as a timeout exception
if (http.StackTrace.Contains("GetEntireRawContent"))
{
// MAX REQUEST HAS BEEN EXCEEDED
return true;
}
}
return false;
}
TA贡献2065条经验 获得超14个赞
正如GateKiller所说,你需要改变maxRequestLength。如果上传速度太慢,您可能还需要更改executionTimeout。请注意,您不希望这些设置中的任何一个太大,否则您将对DOS攻击开放。
executionTimeout的默认值为360秒或6分钟。
您可以使用httpRuntime元素更改maxRequestLength和executionTimeout 。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<httpRuntime maxRequestLength="102400" executionTimeout="1200" />
</system.web>
</configuration>
编辑:
如果你想要处理异常,那么就像已经说明的那样,你需要在Global.asax中处理它。这是代码示例的链接。
- 3 回答
- 0 关注
- 433 浏览
添加回答
举报