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

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

问题描述

我限制了用户可以从 Web.config 上传到站点的文件大小.如此处所述,如果不接受大小,它应该抛出 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 事件中捕获它,但无论我做什么,异常都未得到处理.代码如下:

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

但我仍然明白:

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.

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

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