[Resolving] Maximum request length exceeded exception

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

System.Web.HttpException: Maximum request length exceeded.
A0A0 at System.Web.HttpRequest.GetEntireRawContent()
A0A0 at System.Web.HttpRequest.get_InputStream()
A0A0 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://msdn.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>

3 Comments

  • Thanks.

    Just what I were looking for. :)

  • Thanks! TGF blogs.

  • 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

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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;

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (context.Request.ContentLength &gt; maxRequestLength)

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IServiceProvider provider = (IServiceProvider)context;

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Check if body contains data

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (workerRequest.HasEntityBody())

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// get the total body length

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int requestLength = workerRequest.GetTotalEntityBodyLength();

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Get the initial bytes loaded

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int initialBytes = 0;

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (workerRequest.GetPreloadedEntityBody() != null)

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;initialBytes = workerRequest.GetPreloadedEntityBody().Length;

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (!workerRequest.IsEntireEntityBodyIsPreloaded())

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;byte[] buffer = new byte[512000];

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Set the received bytes to initial bytes before start reading

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int receivedBytes = initialBytes;

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;while (requestLength - receivedBytes &gt;= initialBytes)

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Read another set of bytes

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Update the received bytes

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;receivedBytes += initialBytes;

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

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

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

    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

    }

Comments have been disabled for this content.