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
Best Answer
When running under IIS7 and upwards there is another parameter:
The default setting is slightly less than 30 MB.
For uploaded files with size between
maxRequestLength
andmaxAllowedContentLength
IIS7 will throw anHttpException
with HTTP code 500 and message textMaximum request length exceeded
. When this exception is thrown, IIS7 kills the connection immediately. So anHttpModule
that redirects on this error will only work if theHttpException
is handled and cleared (usingServer.ClearError()
) inApplication_Error()
in global.asax.cs.For uploaded files with size bigger than
maxAllowedContentLength
IIS7 will display a detailed error page with error code 404 andsubStatusCode
13. The error page can be found in C:\inetpub\custerr\en-US\404-13.htmFor redirects on this error on IIS7 I recommend redirecting on
httpErrors
instead. To redirect to a different action set a smaller value formaxAllowedContentLength
thanmaxRequestLength
in web.config and also add the following to web.config: