我限制了用户可以从Web.config上传到网站的文件大小。正如解释过的这里一样,如果不接受大小,它应该抛出一个ConfigurationErrorsException。我试图从动作方法或控制器获取它,以上传请求,但没有成功。连接被重新检查,我无法让它显示错误页面。
我试着在BeginRequest事件中捕获它,但是不管我做什么,异常都是无法处理的。下面是代码:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
try
{
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[512];
// 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);
}
}
}
}
catch(HttpException)
{
context.Response.Redirect(this.Request.Url.LocalPath + "?action=exception");
}
}但我还是明白了:
Maximum request length exceeded.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Maximum request length exceeded.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.更新:
哪种方法会引发异常?如果我读取请求,它会引发异常,如果我根本不读取它,我将在浏览器中获得"101连接重置“。这里能做些什么?
发布于 2011-04-28 12:44:15
如果没有客户端的帮助,就没有办法做好这件事。除非您阅读所有请求,否则无法确定请求是否太长。如果你把每个请求读到最后,任何人都会来让你的服务器忙着。如果您只查看内容长度并删除请求,则另一方会认为存在连接问题。处理错误是无能为力的,这是HTTP的一个缺点。
您可以使用Flash或Javascript组件来修复它,因为它不能很好地失败。
发布于 2010-05-03 07:35:24
您无法在操作方法中捕获错误,因为异常来得更早,但您可以在这里捕获它。
protected void Application_Error() {
var lastError = Server.GetLastError();
if(lastError !=null && lastError is HttpException && lastError.Message.Contains("exceed")) {
Response.Redirect("~/errors/RequestLengthExceeded");
}
} 实际上,当文件大小超过限制时,就会出现HttpException错误。
在应用程序中,也存在IIS对内容的限制。IIS 7抛出
HTTP错误404.13 -未找到请求过滤模块被配置为拒绝超过请求内容长度的请求。
你可以搜索它,有很多关于这个iis错误的信息。
发布于 2011-01-29 19:47:55
我对此不是百分之百,但我认为如果你试着改变一下,也许会有帮助:
context.Response.Redirect(this.Request.Url.LocalPath + "?action=exception");
至
Server.Transfer(this.Request.Url.LocalPath + "?action=exception,false)
我的想法是,over请求长度请求仍在重定向调用中进行处理,但是如果您告诉它丢弃表单数据,它将处于最大请求长度之下,然后它的行为可能会有所不同。
没有保证,但这很容易检查。
https://stackoverflow.com/questions/2756448
复制相似问题