Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET - how to show a error page when uploading big file (Maximum request length exceeded)?

Application able to record error in OnError, but we are not able to do any redirect or so to show something meaningfull to user. Any ideas? I know that we can set maxRequestLength in web.config, but anyway user can exceed this limit and some normal error need to be displayed.

like image 593
st78 Avatar asked Sep 24 '08 09:09

st78


People also ask

How do you fix the page was not displayed because the request entity is too large?

Clear your Constant Contact cookies to fix the Request Entity Too Large error. Occasionally when navigating your account, you might see an error message that reads "Request Entity Too Large." When this happens, it means that your Constant Contact cookies have built up and need to be cleared.

What is httpRuntime maxRequestLength?

HttpRuntime maxRequestLength ASP.NET has its own setting to limit the size of uploads and requests. Use the maxRequestLength of the httpRuntime element. The default size is 4096 kilobytes (4 MB). Max value 2,147,483,647 kilobytes (~82 Terabyte).

What is maxAllowedContentLength?

Description. maxAllowedContentLength. Optional uint attribute. Specifies the maximum length of content in a request, in bytes. The default value is 30000000 , which is approximately 28.6MB.

How do you handle Maximum request length exceeded?

The default maximum filesize is 4MB - this is done to prevent denial of service attacks in which an attacker submitted one or more huge files which overwhelmed server resources. If a user uploads a file larger than 4MB, they'll get an error message: "Maximum request length exceeded." Server Error in '/' Application.


1 Answers

As you say you can setup maxRequestLength in your web.config (overriding the default 4MB of your machine.config) and if this limits is exceeded you usually get a HTTP 401.1 error.

In order to handle a generic HTTP error at application level you can setup a CustomError section in your web.config within the system.web section:

<system.web>
   <customErrors mode=On defaultRedirect=yourCustomErrorPage.aspx />
</system.web>

Everytime the error is showed the user will be redirected to your custom error page.

If you want a specialized page for each error you can do something like:

<system.web>    
   <customErrors mode="On" defaultRedirect="yourCustomErrorPage.aspx">
     <error statusCode="404" redirect="PageNotFound.aspx" />
   </customErrors>
</system.web>

And so on.

Alternatively you could edit the CustomErrors tab of your virtual directory properties from IIS to point to your error handling pages of choice.

The above doesn't seem to work for 401.x errors - this code-project article explains a workaround for a what seems to be a very similar problem: Redirecting to custom 401 page

like image 96
JohnIdol Avatar answered Sep 27 '22 21:09

JohnIdol