web analytics

"Maximum request length exceeded" Exception

Options

codeling 1602 - 6666
@2017-01-18 21:12:02

Try to upload a file using ASP.NET FileUpload control, the following excpetion is thrown:

System.Web.HttpException: Maximum request length exceeded.

The request filtering module is configured to deny a request that exceeds the request content length.

@2017-01-18 21:12:49

By default, the maximum size of a file to be uploaded to the server using the FileUpload control is around 4MB.
This value can be increased by modifying the maxRequestLength attribute in the web application’s configuration file (web.config)

In addition, the default maximum length of content in a request supported by IIS is around 28.6 MB.
This value can be increased by modifying the maxAllowedContentLength attribute in the web application’s configuration file (web.config)

The way the pipeline works is that the IIS value (maxAllowedContentLength) is checked first, then the ASP.NET value (maxRequestLength) is checked.
If the content exceeds the IIS value (maxAllowedContentLength), then the user will get a 404.13 error page.
If the content exceeds the ASP.NET value (maxRequestLength), then a exception is thrown (System.Web.HttpException: Maximum request length exceeded.)
This excepton is typically caught by the web app, and is handled based on the <customErrors> setting (under <system.web>)

@2017-01-18 21:14:02

First, you do need to make sure that the ASP.NET method of limiting the request length (maxRequestLength) is greater than or equal to the IIS method of limiting the request length (maxAllowedContentLength).

So, if you want to restrict uploads to 10MB, set maxRequestLength to “10240” (or greater) and maxAllowedContentLength to "10485760".
Again, this essentially prevents the ASP.NET HTTP runtime from complaining about a request length that exceeds its limits, since the IIS feature will limit the length before ASP.NET has a chance to.

If the content exceeds the IIS value (maxAllowedContentLength), then the user will get a 404.13 error page.

In the web.config, you can add an override to this particular status and sub-status to tell IIS to redirect this particular status to a certain page.

This configuration block will prevent IIS from displaying it’s default (read: user unfriendly) error page for 404.13, and allow you to redirect them to a more useful page that informs them they’ve uploaded a file that is too large.

Be advised that the path attribute of error is an absolute path (you can’t reference the application root using ~) when the responseMode is set to "Redirect".

Finally, remember that maxRequestLength is in KB whereas maxAllowedContentLength is in bytes!

Putting all this together, your web.config file would look something like this:

<system.web>
   <!-- maxRequestLength for asp.net, in KB --> 
   <httpRuntime maxRequestLength="10241" /> 
   <customErrors mode="RemoteOnly" redirectMode="ResponseRedirect" defaultRedirect="~/ErrorPage.aspx"/>
</system.web>
<system.webServer>
   <httpErrors errorMode="Custom" >
      <remove statusCode="404" subStatusCode="13" /> 
      <error statusCode="404" subStatusCode="13" prefixLanguageFilePath="" path="/mobile/ContentTooLarge.html" responseMode="Redirect" /> 
   </httpErrors>            
   <security> 
      <requestFiltering> 
         <!-- maxAllowedContentLength, for IIS, in bytes --> 
         <requestLimits maxAllowedContentLength="10485760" />
      </requestFiltering> 
   </security> 

The above setting will set the maximum request length to 10MB.

@2017-01-19 11:50:57

Use the following C# code to access the request length setting programmatically.

long requestLength = 4096; // assume default value

HttpRuntimeSection runTime = ConfigurationManager.GetSection("system.web/httpRuntime") as HttpRuntimeSection; // check web.config

if (runTime != null)

{

    requestLength = runTime.MaxRequestLength;

}

else

{

     // not found...check machine.config

     Configuration cfg = ConfigurationManager.OpenMachineConfiguration();

     ConfigurationSection cs = cfg.SectionGroups["system.web"].Sections["httpRuntime"];

     if (cs != null)

     {

           requestLength = Convert.ToInt64(cs.ElementInformation.Properties["maxRequestLength"].Value);

     }

}

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com