Display Custom Error Page When File Upload Exceeds Allowed Size in ASP.NET MVC

Display custom error page when file upload exceeds allowed size in ASP.NET MVC

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>

Displaying a meaningfull error when uploading too large a file

I had this same problem and after lots of searching there seems to be no perfect solution to this. You can change the Max Request Length to set at which point IIS will serve the ASP.Net error. You can also catch this error on global.asax and show the user a more freindly error page.

You can find more information on doing this here http://www.developer.com/db/article.php/10920_3426051_2

Custom error page for Http error 404.13 ASP.NET Core MVC

You are correct. IIS is nabbing the error before it gets into your pipeline. I would recommend adding the httpErrors module into your web.config and pointing it at a page on the site.

<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="13" />
<error statusCode="404"
subStatusCode="13"
prefixLanguageFilePath=""
path="http://yourwebsite.com/path/to/page"
responseMode="Redirect" />
</httpErrors>
</system.webServer>

ASP.NET MVC app custom error pages not displaying in shared hosting environment

I've found the solution and it's incredibly simple. Turns out the problem was actually in IIS7. While debugging this issue in Visual Studio I saw a property of the HttpResponse object that I hadn't noticed before:

public bool TrySkipIisCustomErrors { get; set; }

This lead me to my nearest search engine which turned up a great blog post by Rick Strahl and another on angrypets.com as well as this question here on SO. These links explain the gory details much better than I can, but this quote from Rick's post captures it pretty well:

The real confusion here occurs because the error is trapped by
ASP.NET, but then ultimately still handled by IIS which looks at the
500 status code and returns the stock IIS error page.

It also seems this behavior is specific to IIS7 in Integrated mode. From msdn:

When running in Classic mode in IIS 7.0 the TrySkipIisCustomErrors
property default value is true. When running in Integrated mode, the
TrySkipIisCustomErrors property default value is false.

So essentially all I ended up having to do is add Response.TrySkipIisCustomErrors = true; right after any code that sets the Response.StatusCode to 500 or 503 and everything now functions as designed.

redirect to error on exceeding fileupload size

Check this question: Display custom error page when file upload exceeds allowed size in ASP.NET MVC

Displaying an error message for System.Web.HttpException: Maximum request length exceeded in WebMatrix

I have managed to fix this myself by using the event handler in Global.asax



Related Topics



Leave a reply



Submit