Catching "Maximum Request Length Exceeded"

Catching Maximum request length exceeded

There is no easy way to catch such exception unfortunately. What I do is either override the OnError method at the page level or the Application_Error in global.asax, then check if it was a Max Request failure and, if so, transfer to an error page.

protected override void OnError(EventArgs e) .....

private void Application_Error(object sender, EventArgs e)
{
if (GlobalHelper.IsMaxRequestExceededException(this.Server.GetLastError()))
{
this.Server.ClearError();
this.Server.Transfer("~/error/UploadTooLarge.aspx");
}
}

It's a hack but the code below works for me

const int TimedOutExceptionCode = -2147467259;
public static bool IsMaxRequestExceededException(Exception e)
{
// unhandled errors = caught at global.ascx level
// http exception = caught at page level

Exception main;
var unhandled = e as HttpUnhandledException;

if (unhandled != null && unhandled.ErrorCode == TimedOutExceptionCode)
{
main = unhandled.InnerException;
}
else
{
main = e;
}

var http = main as HttpException;

if (http != null && http.ErrorCode == TimedOutExceptionCode)
{
// hack: no real method of identifying if the error is max request exceeded as
// it is treated as a timeout exception
if (http.StackTrace.Contains("GetEntireRawContent"))
{
// MAX REQUEST HAS BEEN EXCEEDED
return true;
}
}

return false;
}

Maximum request length exceeded in DOT NETCORE MVC c#

For 500mb videos use the code below:

<httpRuntime maxRequestLength="2147483647" />

In simple terms you even need a higher memory but for images and videos around 2.5MB this will fix it. Anything higher will require a higher maxRequestLength.

Or even increase it to something like

<httpRuntime maxRequestLength="22147483647" />

I used this to handle pictures that are higher and manipulate by reducing the image size in code and it worked.

Maximum request length exceeded exception on postback

A postback sends back the viewstate of every control - if you have a huge datagrid, then when the browser reposts that to the server, this is why you are getting the exception.

Your two options are:

  1. Set EnableViewState="false" on your GridView if you do not need the viewstate, so it's not so bloated and the postback is a reasonable size,
  2. Increase the maximum request size in web.config as shown below:

    <configuration>
    <system.web>
    <httpRuntime maxRequestLength="32768" />
    </system.web>
    </configuration>

Hope this helps

Maximum request length exceeded error even after setting maxAllowedContentLength

requestLimits settings have been added since IIS 7.0.

For IIS 6 you need to use:

<system.web>
<httpRuntime maxRequestLength="1048576" executionTimeout="100000" />
</system.web>

This allows a file upload of 1 GB and it will time out after 100,000 seconds, or 27.8 hours.

Maximum request length exceeded.

If you are using IIS for hosting your application, then the default upload file size is 4MB. To increase it, please use this below section in your web.config -

<configuration>
<system.web>
<httpRuntime maxRequestLength="1048576" />
</system.web>
</configuration>

For IIS7 and above, you also need to add the lines below:

 <system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>

Note:

  • maxRequestLength is measured in kilobytes
  • maxAllowedContentLength is measured in bytes

which is why the values differ in this config example. (Both are equivalent to 1 GB.)



Related Topics



Leave a reply



Submit