Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using Visual Studio 2010/ASP.NET/C-Sharp website.

I basically had an ASP.NET FileUpload control, for which I needed to cater the exception thrown for the following message:-
Maximum request length exceeded.

The maximum file size setting is set in the web.config as follows:-
XML
<system.webServer>
    <security>
          <requestFiltering>
        <requestLimits maxAllowedContentLength="41943040"/>
        </requestFiltering>
    </security>
</system.webServer>


And
XML
<system.web>
    <httpRuntime maxRequestLength="40960" requestValidationMode="2.0" />
</system.web>


Using Global.asax with validating file size in "Application_Error()", but it has not resolved my issue and it crashes at the Redirect when file size is greater and a redirect to Error Page is not working.


I have used the following code, although it is now running the Application_Error() Code section, but the problem is that it is not redirecting to the About.aspx page.

C#
void Application_Error(object sender, EventArgs e)
  {
      // Code that runs when an unhandled error occurs
      Exception exc = Server.GetLastError();
      try
      {
          if (exc.Message.Contains("Maximum request length exceeded"))
          {
              Response.Redirect("~/About.aspx", false);
          }
          if (exc.InnerException.Message.Contains("Maximum request length exceeded"))
          {
              Response.Redirect("~/About.aspx", false);

          }
      }
      catch (Exception ex)
      {

      }
  }

I want to find out that how can I accomplish my requirements, since I am stuck for this since a couple of days.
Kindly help me in this regards as early as possible.
Posted
Comments
Sandeep Mewara 24-Feb-13 3:37am    
So, execution does come in Application_Error when length exceeds, right?
Muzaffar Ali Rana 25-Feb-13 3:55am    
Yes.
But after coming into the Application_Error code block, the page is not redirected.
Although I have used

Response.Clear();
and then Response.Redirect("~/About.aspx");
But its not working.

And similarly I have used
Server.Clear(); with
Server.Transfer("About.aspx"); But still it is not working too.
Sandeep Mewara 26-Feb-13 0:21am    
Could you please remove try-catch from this method. Just log the error or plain and simple redirect it.
Muzaffar Ali Rana 26-Feb-13 8:46am    
I removed the Try-Catch from this method, but it is still not working for me.
Muzaffar Ali Rana 8-Mar-13 6:06am    
Is there anybody to help me out this issue?
Thanks!

1 solution

You have to clear the error object before the redirect.

i.e.

C#
HttpContext.Current.ClearError();
Response.Redirect("~/About.aspx", false);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900