Hosam Kamel's Blog

Sponsors

News


Follow HosamKamel on Twitter

Blogroll

[Resolving] Maximum request length exceeded exception

You will get such an exception if you are trying to upload large files or making a request  which exceeded 4MB.

System.Web.HttpException: Maximum request length exceeded.
   at System.Web.HttpRequest.GetEntireRawContent()
   at System.Web.HttpRequest.get_InputStream()
   at com...ProcessRequest(HttpContext context)

The problem is that .NET limits the maximum data sent in each request to 4096 KB (4MB) by default.

you can take a look on httpRuntime Elements here http://msdn2.microsoft.com/en-us/library/e1f13641.aspx

To resolve this issue : try to increase the request size

 1: <configuration>
 2:  <system.web>
 3:  <httpRuntime maxRequestLength="51200"
 4:  enable = "True"
 5:  executionTimeout="45"/>
 6:  </system.web>
 7: </configuration>

Comments

Emil said:

Thanks.

Just what I were looking for. :)

# September 25, 2008 6:08 AM

Mark said:

Thanks! TGF blogs.

# February 4, 2009 6:34 PM

Rakesh Kumar Roy said:

Hi Guys,

This is the code below to redirect the same page if “Max Request Length Exception” occur. Just write this code on global.asax.

If the page content size is greater than maxRequestLength then this code redirect the page to the same page with query string action=exception. Just read this query string value and show the proper message the client browser.

protected void Application_BeginRequest(Object sender, EventArgs e)

{

HttpRuntimeSection runTime = (HttpRuntimeSection)WebConfigurationManager.GetSection("system.web/httpRuntime");

//Approx 100 Kb(for page content) size has been deducted because the maxRequestLength proprty is the page size, not only the file upload size

           int maxRequestLength = (runTime.MaxRequestLength - 100) * 1024;

//This code is used to check the request length of the page and if the request length is greater than

//MaxRequestLength then retrun to the same page with extra query string value action=exception

HttpContext context = ((HttpApplication)sender).Context;

           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[512000];

                       // 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);

                   }

               }

               // Redirect the user to the same page with querystring action=exception.

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

           }

}

# February 13, 2009 1:43 PM

Gary said:

Many Thanks

# April 3, 2009 1:18 AM

Content Deployment error: Maximum request length exceeded. « The SharePoint Nomad said:

Pingback from  Content Deployment error: Maximum request length exceeded. &laquo; The SharePoint Nomad

# December 19, 2009 11:58 AM

Maximum request length exceeded exception | sveroa's developer blog said:

Pingback from  Maximum request length exceeded exception  | sveroa&#039;s developer blog

# February 15, 2011 2:51 AM

wcf config issues | Imran's Blog said:

Pingback from  wcf config issues | Imran&#039;s Blog

# August 21, 2011 8:11 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)