Developing Matt

My Technical Journal

Maximum request length exceeded and helping users past Microsoft Speak

leave a comment »

Using file attachments can cause problems if your file attachments are larger than designated.  By default the designated amount is 4MB.  This can be modified in the web.config.  I have it set up to allow a 20 MB file

         <httpRuntime maxRequestLength="20480" executionTimeout="200" />

If the user exceeds the size dictated in the web.config they will receive the following. 

Server Error
Maximum request length exceeded.

Most users don’t understand Microsoft speak, so it is up to you to help them.  To deliver a friendly message you could write your own tool to upload but that would be a lot of effort for something microsoft already does for you.  The problem is that the error occurs at the application level, not the page level. You can Try Catch all day long but they will never get your friendly popup that you worked so hard on. 

The elegant solution is to add something to the Application_Error routine in the global.asax.  I catch the error and forward it to an Error page, passing along my own querystrings (error=filelength and jobid…something pertinent to my app)

    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        Dim x As Exception = Server.GetLastError

        If x.InnerException.Message = "Maximum request length exceeded." Then


            Dim jobid As String = Context.Request.QueryString.Item("jobid")

            Response.Redirect("Error.aspx?error=filelength&jobid=" & jobid, True)

        End If

    End Sub

In the Error.aspx page I catch the query strings and pass along the pertinent information.  I set it up to enable the catch of other errors at the application level, but so far this is the only error I catch.

Code Behind:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Request.QueryString.Item("error") = "filelength" Then lblFileSize.Visible = True

        linkReturn.NavigateUrl = "job.aspx?jobid=" & Request.QueryString.Item("jobid").ToString

    End Sub

HTML Markup:

    <form id="form1" runat="server">

        <asp:Label ID="lblFileSize" runat="server"

            Text="There was an error with the attachment. The file size is too big (greater than 20 MB). 

Please find another way to transfer your file (shrink and/or zip your attachment, for example)."


        <br />

        <br />

        <asp:Label ID="Label1" runat="server" Text="Thank you." Font-Size="Large"></asp:Label>

        <br />

    <br />

    <asp:HyperLink ID="linkReturn" runat="server" Font-Size="Large">Click here to return</asp:HyperLink>


Thank you Austin Rasmussen for your post here.  Yours was the most helpful page out there that assisted me in this venture.  I adopted a slightly different code as my error was not at the HttpException, but at the Exception namespace instead. 


Written by matt

January 16, 2012 at 4:56 pm

Posted in

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: