如何捕捉ConfigurationErrorsException违反了maxRequestLength? [英] How to catch ConfigurationErrorsException for violating maxRequestLength?

查看:176
本文介绍了如何捕捉ConfigurationErrorsException违反了maxRequestLength?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我限制文件大小的用户可以上传从Web.config中的网站。正如所解释的 rel=\"nofollow\">,它应该抛出一个ConfigurationErrorsException如果大小不被接受。我试图从操作方法或控制器上传请求,但没有运气抓住它。连接被重置了,我不能得到它显示一个错误页面。

I am limiting file size users can upload to the site from Web.config. As explained here, it should throw a ConfigurationErrorsException if size is not accepted. I tried to catch it from the action method or controller for upload requests but no luck. Connection is resetted and I can't get it to show an error page.

我试着在BeginRequest事件追赶,但无论我做什么异常是未处理。
这里的code:

I tried catching it in BeginRequest event but no matter what I do the exception is unhandled. Here's the code:

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");
    }
}

但我还是得到这样的:

But I still get this:

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连接重置,在浏览器中。有什么可以在这里完成?

What method raises the exception anyway? If I read the request it raises exception If I don't read it at all, I get "101 Connection Reset" in browser. What can be done here?

推荐答案

有没有办法把事情做对没有客户端的帮助。你不能确定是否这个请求太长,除非你读它的全部。如果你读每个请求结束时,有人进来,让您的服​​务器忙。如果你只是看内容长度和删除请求,对方是要认为这是一个连接问题。这是什么可以与错误处理做的,这是HTTP的一个缺点。

There is no way to do it right without a client-side help. You cannot determine if the request is too long unless you read all of it. If you read each request to the end, anyone come and keep your server busy. If you just look at content length and drop the request, other side is going to think there is a connection problem. It's nothing you can do with error handling, it's a shortcoming of HTTP.

您可以使用Flash或Javascript组件,使其正确的,因为这个东西不能很好地失败。

You can use Flash or Javascript components to make it right because this thing can't fail nicely.

这篇关于如何捕捉ConfigurationErrorsException违反了maxRequestLength?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆