Catch System.Web.HttpException: Maximum request length exceeded

Topics: Web Client Software Factory
Jul 1, 2009 at 10:45 PM

Hello!

I'm facing the following issue: When trying to upload a large file, I get System.Web.HttpException: Maximum request length exceeded.

I have:

    public class GlobalExceptionLogger : IHttpModule
    {
        private Logger logger;

        public void Dispose()
        {
        }
        
        public void Init(HttpApplication context)
        {
            context.Error += new EventHandler(this.OnUnhandledException);
            this.logger = new Logger();
        }

        private void OnUnhandledException(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;

            Exception ex = application.Server.GetLastError().GetBaseException();         

            logger.Error(
                ex, 
                "General", 
                string.Format("Request Path: {0}\nRequest Size: {1}\nContent Length: {2}", application.Request.Path, application.Request.TotalBytes, application.Request.ContentLength));

            if (application.Context.Items["PaginaErro"] != null)
            {
                application.Server.Transfer("PaginaErro.aspx");
            }
            else
            {
                application.Server.Transfer("~/PaginaErro.aspx");
            }
        }
    }

Which works fine with every other exception I was able to generate. I also tried catching it in Global.asax. but it does not work as well.

Any ideas of what could be causing this?

Thanks!


Jul 3, 2009 at 8:11 PM

Hi tatovisk85,

 

I think I was able to reproduce the issue you mention and found the error.

I tried catching the exception in the Global.asax and debugging I found that the code was executing. Using the code you posted and debugging the Application_Error method in the global.asax I found that when the exception you mentioned is thrown, the application.Request.TotalBytes property is not set. If you try to access this property a new exception is thrown.

As explained in the thread Application_Error not firing, if an exception occurs in the execution of the Application_Error method, these exception will not be detected and will appears as if the method would not have been executed.

 

Hope it helps!

 

Matias Bonaventura

http://blogs.southworks.net/matiasb