Catching Exception for "Maximum request length exceeded"

C

Chris Hayes

Greetings,

I have an ASP.NET page that accepts input from an HtmlInputFile object.

I have set the maximum size for HttpRequests to 2MB in the web.config file.

And now I am trying to CATCH the "Maximum request length exceeded"
HttpException when a user tries to send a file that is greater than 2MB so
that I may tell the user, gracefully, that their file is too big.

Now, my question is where in the world do I add a Handler for HttpException,
apparrently I'm not smart enough to know where this goes or to find any kind
of useful article on the subject (all the ones I have found, don't address
what I am trying to do, and worse yet don't give any real clear examples
just vague descriptions).

Thanks,

Chris
 
C

Chris Hayes

I figured it out...

I placed the code to handle it in the Application_Error method of the
global.asax file... I tend to overlook the global.asax nowadays...

Chris
 
J

Juan T. Llibre

Hi, Chris.

This should work...

In global.asax :
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
Server.Transfer("Errors.aspx")
End Sub

The "errors.aspx" :
----------------------------
<html>
<script language="VB" runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim errMessage As String = ""
Dim appException As System.Exception = Server.GetLastError()
If (TypeOf (appException) Is HttpException) Then
Dim checkException As HttpException = CType(appException, HttpException)
Select Case checkException.GetHttpCode
Case 403
errMessage &= "You are not allowed to view that page."
Case 404
errMessage &= "The page you have requested can't be found."
Case 408
errMessage &= "The request has timed out."
Case 500
errMessage &= "The server can't fulfill your request."
Case Else
errMessage &= "The server has experienced an error."
End Select
Else
errMessage &= "The following error occurred<BR>" & appException.ToString
End If
ErrorMessage.Text = errMessage & "<BR>Please contact the server's administrator."
Server.ClearError()
End Sub
</script>

<body>
<hr>
<asp:label id="ErrorMessage" font-size="12" font-bold="true" runat=server/>
<hr>
</body>
</html>

--------------------

You can embellish that a bit, or add info ( like an email address )
and I'm sure you can improve on that basic structure, but at least
this handler works when an httpException is thrown.




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
J

Juan T. Llibre

Chris,

Just add this case to the code I just sent you
in the "Well global.asax didn't work as well as hoped" thread :

Case 400
errMessage &= "Bad request. The file size is too large."

That will work fine for what you want to do.



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
C

Chris Hayes

Thanks Juan,

I tried what you provided and it worked up to a point, and then all kinds of
problems occurred again. It's not because of the code, it's because of how
ASP.NET handles Requests that exceed the maximum size.

I finally went with setting a custom error page for HTTP 400 (Request
exceeds maximum length). It doesn't allow me to do everything I want, but at
least it handles the error more gracefully.

Thanks again,

Chris

PS
I'm going to keep working on this (probably off and on). I want a more user
friendly handler for HTTP 400.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top