42

My main issue is that I want to display an custom error page when an uploaded file exceeds allowed size (maxRequestLength in web.config).

When the big file is uploaded an HttpException is thrown before my upload action method in the controller is invoked. This is expected.

I have tried to catch the exception in a custom attribute and also to override OnException in the controller. Why isnt it possible to catch the exception in either the attribute or the OnException method?

Its possible though to catch the exception in Application_Error in global.asax but neither Response.Redirect nor Server.Transfer works for redirecting to the custom error page. Server.Transfer gives the "failed to process child request" error and response.redirect gives the "Http headers already sent" error.

Any ideas?

Thanks in advance!

Marcus

Marcus
  • 2,470
  • 1
  • 22
  • 29
  • 3
    Not an answer to your question. I use SWFUpload (http://www.swfupload.org/) and set a file size limit. This way the client will not be able to even start uploading a file greater than the limit. You set the limit in JavaScript like so: file_size_limit : "20 MB". See doc: http://demo.swfupload.org/Documentation – Raj Kaimal May 03 '10 at 15:55

4 Answers4

60

When running under IIS7 and upwards there is another parameter:

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="10485760" />
    </requestFiltering>
  </security>
</system.webServer>

The default setting is slightly less than 30 MB.

For uploaded files with size between maxRequestLength and maxAllowedContentLength IIS7 will throw an HttpException with HTTP code 500 and message text Maximum request length exceeded. When this exception is thrown, IIS7 kills the connection immediately. So an HttpModule that redirects on this error will only work if the HttpException is handled and cleared (using Server.ClearError()) in Application_Error() in global.asax.cs.

For uploaded files with size bigger than maxAllowedContentLength IIS7 will display a detailed error page with error code 404 and subStatusCode 13. The error page can be found in C:\inetpub\custerr\en-US\404-13.htm

For redirects on this error on IIS7 I recommend redirecting on httpErrors instead. To redirect to a different action set a smaller value for maxAllowedContentLength than maxRequestLength in web.config and also add the following to web.config:

<system.webServer>
  <httpErrors errorMode="Custom" existingResponse="Replace"> 
    <remove statusCode="404" subStatusCode="13" /> 
    <error statusCode="404" subStatusCode="13" prefixLanguageFilePath=""
       path="http://yoursite.com/Error/UploadTooLarge" responseMode="Redirect" /> 
  </httpErrors>
</system.webServer>
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
Marcus
  • 2,470
  • 1
  • 22
  • 29
  • How do you do this with IIS6? – Jonathan May 10 '11 at 12:46
  • @Jonathan: See this answer http://stackoverflow.com/questions/2759193/display-custom-error-page-when-file-upload-exceeds-allowed-size-in-asp-net-mvc2/2856886#2856886 – Marcus May 27 '11 at 14:20
  • 1
    An important thing to note is that if you want to _avoid_ having to deal with the HTTP 500 _Maximum request length exceeded_ errors altogether, you should set both `maxRequestLength` and `maxAllowedContentLength` to the _same size_ (the former should be expressed in kilobytes while the latter in bytes as [explained here](http://stackoverflow.com/a/3853785/26396)). This way there won't be a request whose size falls between the two values, they can only ever be either smaller or larger than the threshold. – Enrico Campidoglio Jun 30 '15 at 08:35
  • 2
    responseMode="Redirect" is required for 404.13 error. It simply does not work in ExecuteURL mode. – Der_Meister Jan 19 '16 at 05:58
3

When running on IIS6, I solved it with a HttpModule by handling the BeginRequest and check if httpApplication.Context.Request.Length is larger than maxRequestLength.

To be able to redirect the entire request has to be read before redirecting.

See code example at this link: http://www.velocityreviews.com/forums/t97027-how-to-handle-maximum-request-length-exceeded-exception.html

Marcus
  • 2,470
  • 1
  • 22
  • 29
  • 2
    The negative part of this solution is that the entire request (and file) needs to be read before the redirection can be done. – Marcus May 25 '10 at 09:03
  • That's not a bad thing though if that's the only way. What's the alternative? First, is it possible to detect maxRequestLength exceeded errors before the entire request has been read? This event would have to be captured in code, so you can run some preparation code and call Server.Transfer. Second, if you can do something before the entire request is read, and you cancel it somehow, how with the user's web browser respond? Will it think the connection failed? – Triynko Apr 12 '11 at 21:10
1

The velocity eviews link was really helpful in solving the issue. As stated, the only drawback was the entire request (and file) needs to be read before the redirection can be done.

But it can be limited to run only when the page where the file upload control is present by being loaded like this

if (HttpContext.Current.Request.Url.ToString().Contains("UploadedPage.aspx") 
{
    //read and process page request
}
aLearner
  • 1,051
  • 14
  • 30
user455406
  • 11
  • 1
0

You need to make a custom HttpHandler that will do this for you. ASP.NET will automatically kill the connection if the upload size is too large (as you found out).

Tejs
  • 40,736
  • 10
  • 68
  • 86