views:

597

answers:

4

Hello!

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

A: 

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
+1  A: 

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
The negative part of this solution is that the entire request (and file) needs to be read before the redirection can be done.
Marcus
+1  A: 

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 30mb.

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

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 valuer for maxAllowedContentLength than maxRequestLength in web.config and also add the following to web.confg:

<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>
Marcus