如何捕获ConfigurationErrorsException以违反maxRequestLength?

我限制文件大小用户可以从Web.config上传到站点。 如此处所述,如果不接受大小,它应抛出ConfigurationErrorsException。 我试图从动作方法或控制器中捕获上传请求,但没有运气。 连接被重置,我无法显示错误页面。

我尝试在BeginRequest事件中捕获它,但无论我做什么,exception都是未处理的。 这是代码:

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. 

更新:

无论如何,什么方法引发了exception? 如果我读取请求它会引发exception如果我根本不读它,我在浏览器中得到“101 Connection Reset”。 这可以做什么?

你不能在行动方法中发现错误因为exception来得更早,但你可以在这里抓住它

 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 – 未找到请求筛选模块配置为拒绝超过请求内容长度的请求。

你可以google它,有很多关于这个iis错误的信息。

没有客户端的帮助,没有办法做到这一点。 除非您阅读全部请求,否则无法确定请求是否过长。 如果您将每个请求都读到最后,那么任何人都会让您的服务器忙碌。 如果您只是查看内容长度并删除请求,则另一方会认为存在连接问题。 这与error handling无关,这是HTTP的缺点。

你可以使用Flash或Javascript组件来使它正确,因为这件事不会很好地失败。

我不是100%对此,但我认为如果你尝试改变它可能会有所帮助:

context.Response.Redirect(this.Request.Url.LocalPath + "?action=exception");

Server.Transfer(this.Request.Url.LocalPath + "?action=exception,false)

我的想法是,仍然在重定向调用中处理超过最大请求长度的请求,但如果您告诉它放弃表单数据,它将变为最大请求长度,然后它可能表现不同。

没有保证,但很容易检查。

  catch (Exception ex) { if (ex is HttpException && (ex as HttpException).WebEventCode == 3004) { //-- you can now inform the client that file uploaded was too large. } else throw; } 

我有一个类似的问题,我想在Application_Error处理程序中捕获“超出最大请求长度”exception,然后执行重定向。

(不同之处在于我正在使用ASP.Net Web API编写REST服务,而不是重定向到错误页面,我想重定向到一个错误控制器,然后返回相应的响应)。

但是,我发现当通过ASP.Net开发服务器运行应用程序时,Response.Redirect似乎没有工作。 Fiddler会声明“ReadResponse()失败:服务器没有为此请求返回响应。”

我的客户端(Chrome的高级REST客户端)只显示“0 NO RESPONSE”。

如果我然后在我的开发机器上通过IIS的本地副本运行应用程序,那么重定向将正常工作!

我不确定我是否可以明确地说Response.Redirect不能在ASP.Net开发服务器上运行,但它肯定不适用于我的情况。

所以,我建议尝试通过IIS而不是IIS Express或开发服务器运行您的应用程序,看看是否得到了不同的结果。

请参阅此链接,了解如何在Visual Studio中为Web项目指定Web服务器:

http://msdn.microsoft.com/en-us/library/ms178108(v=vs.100).aspx