1

I followed these links:

  1. Catching "Maximum request length exceeded" and
  2. ASP.NET - how to show a error page when uploading big file (Maximum request length exceeded)?

to display error page to handle uploading files exceeding the maxRequestLength in web.config

But my problem is, it is not redirected to the error page (the message says that the webpage cannot be displayed ). I do not know what I'm missing.

Here's my Code @ Global.asax:

void Application_Error(object sender, EventArgs e) 
{       
    if (IsMaxRequestLengthExceeded(Server.GetLastError()))
    {
        this.Server.ClearError();
        this.Server.Transfer("~/Error.html");
    }
}

private bool IsMaxRequestLengthExceeded(Exception ex)
{
    Exception main;
    HttpUnhandledException unhandledEx = (HttpUnhandledException)ex;

    if (unhandledEx != null && unhandledEx.ErrorCode == -2147467259)
    {
        main = unhandledEx.InnerException;
    }
    else
    {
        main = unhandledEx;
    }

    HttpException httpException = (HttpException)main;
    if (httpException != null && httpException.ErrorCode == -2147467259)
    {
        if (httpException.StackTrace.Contains("GetEntireRawContent"))
        {
            return true;
        }
    }

    return false;
}

And @ web.config:

<httpRuntime executionTimeout="1200" />
<customErrors defaultRedirect="Error.html" mode="On">
</customErrors>

It found out that when maxRequestLength was not initialized, it is set by default to 4MB. (I didn't set it because it is not important to me.)

Hope you could help me with this. Thanks

Community
  • 1
  • 1
KaeL
  • 3,639
  • 2
  • 28
  • 56
  • Shouldn't "Error.html" in web.config be same as in Global.asax "~/Error.html" ? – Răzvan Flavius Panda Jul 21 '11 at 09:12
  • Hi, using `Error.html` causes error, i need the `~` character to specify the root path of the virtual directory. – KaeL Jul 21 '11 at 09:18
  • Shouldn't it be "~/Error.html" in both places then (web.config also)? – Răzvan Flavius Panda Jul 21 '11 at 09:23
  • I think not, my `web.config` is working as expected with `defaultRedirect="Error.html"` :( – KaeL Jul 21 '11 at 09:26
  • My guess is that "Error.html" is not pointing to the error page and that is why it can't be displayed. Try using "~/Error.html" in web.config also. Check this: [ASP.NET paths](http://nathanaeljones.com/129/types-of-asp-net-paths/) – Răzvan Flavius Panda Jul 21 '11 at 09:51
  • Hi, my `web.config` is working properly, just in the scenario above, when uploading files exceeding the `maxRequestLength`, it doesn't redirect to error page. Other than that, it displays the error page. – KaeL Jul 21 '11 at 12:12
  • Hi, I am testing that, and i have the following error... Unable to cast object of type 'System.Web.HttpCompileException' to type 'System.Web.HttpUnhandledException'....-- somebody knows why? – Diego Apr 05 '16 at 23:46

1 Answers1

1

You can add

 <httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" subStatusCode="13" />
  <error statusCode="404" subStatusCode="13" prefixLanguageFilePath="" path="http://localhost:1899/ErrorUpload.aspx" responseMode="Redirect" />
</httpErrors>

just after

<security>
  <requestFiltering>
    <requestLimits maxAllowedContentLength="5000000" />
  </requestFiltering>
</security>

where you redirect to a Error Page...

Diego
  • 2,238
  • 4
  • 31
  • 68