Maximum Request Length Exceeded Not Redirect on Er

2019-03-01 04:46发布

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

2条回答
倾城 Initia
2楼-- · 2019-03-01 05:20

I was able to find another way to solve maxRequestLength error. I found it in this link:

The solution was posted as comment by Rakesh Kumar Roy (Post Details: Friday, February 13, 2009 1:43 PM). This might be helpful for other programmers out there. :D

查看更多
▲ chillily
3楼-- · 2019-03-01 05:26

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...

查看更多
登录 后发表回答