Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8664075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:09:16+00:00 2026-06-12T17:09:16+00:00

Possible Duplicate: Maximum Request Length Exceeded Not Redirect on Error Page I try to

  • 0

Possible Duplicate:
Maximum Request Length Exceeded Not Redirect on Error Page

I try to redirect user to some error page, when he uploads file that exceed maximum size.

I’ve put in Web.config following line to limit file to 10MB:

<httpRuntime maxRequestLength="10240" executionTimeout="360" />

On my page there’s a simply form with standard ASP file upload control and submit button.
I’ve also defined redirection on Page level (I’ve tried also in Global.asax Application_Error handling, but results are the same):

protected void Page_Error(object sender, EventArgs e)
{
    if (HttpContext.Current.Error is HttpException)
    {
        if ((HttpContext.Current.Error as HttpException).ErrorCode==-2147467259)
        {
            Server.ClearError();
            Response.Redirect("~/Error.aspx");
        }
    }
}

I’ve tried also Server.Transfer() – not working.

When I try to upload file bigger than 10 MB I can debug and see that the code from Page_Error is being fully executed twice: even with Server.ClearError(), but the page is not redirected to Error.aspx. Instead, the standard, ugly “Connection has been reset” error page appears.

This code works OK if the error is another type, like division by 0 set on Page_Load. Can you tell me what am I doing wrong here?

BTW. I use Visual Web Developer 2010 Express with .NET 4.0, WindowsXP. Testing on buld-in to VWD IIS server.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T17:09:17+00:00Added an answer on June 12, 2026 at 5:09 pm

    Answer:
    OK, so the answer I’ve found is following: it can’t be done via “normal” exception handling. Folowing code, found in one of mentioned link, put in global.asax, is some wokaround:

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
        System.Web.Configuration.HttpRuntimeSection runTime = (System.Web.Configuration.HttpRuntimeSection)System.Web.Configuration.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
        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;
        if (context.Request.ContentLength > maxRequestLength)
        {
            IServiceProvider provider = (IServiceProvider)context;
            HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
    
            // Check if body contains data
            if (workerRequest.HasEntityBody())
            {
                // get the total body length
                int requestLength = workerRequest.GetTotalEntityBodyLength();
    
                // Get the initial bytes loaded
                int initialBytes = 0;
    
                if (workerRequest.GetPreloadedEntityBody() != null)
                    initialBytes = workerRequest.GetPreloadedEntityBody().Length;
    
                if (!workerRequest.IsEntireEntityBodyIsPreloaded())
                {
                    byte[] buffer = new byte[512000];
    
                    // Set the received bytes to initial bytes before start reading
                    int receivedBytes = initialBytes;
    
                    while (requestLength - receivedBytes >= initialBytes)
                    {
                        // Read another set of bytes
                        initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
    
                        // Update the received bytes
                        receivedBytes += initialBytes;
                    }
                    initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
                }
            }
    
            context.Server.ClearError();  //otherwise redirect will not work as expected
            // Redirect the user
            context.Response.Redirect("~/Error.aspx");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: maximum length of HTTP GET request? how many characters can be sent
Possible Duplicate: Maximum length of the textual representation of an IPv6 address? What would
Possible Duplicate: Maximum length of a std::basic_string<_CharT> string I would like to know how
Possible Duplicate: What is the maximum length of an NSString object? I'm trying to
Possible Duplicate: Mininum and Maximum value of Z-INDEX I see some top z-index in
Possible Duplicate: String's Maximum length in Java - calling length() method I have a
Possible Duplicate: What is the maximum length of a URL? Hello, I want to
Possible Duplicate: How to catch the fatal error: Maximum execution time of 30 seconds
Possible Duplicate: Maximum number of threads per process in Linux? just with some curiosity
Possible Duplicate: How to detect if a user uploaded a file larger than post_max_size?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.