I am building an application with .Net WebApi. I have an endpoint for uploading files and I have setup the web.config as follows:
<httpRuntime targetFramework="4.5" maxRequestLength="300000" />
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="305000000" />
</requestFiltering>
</security>
Currently, if I upload a file that is larger than ~300Mb a 404.13 status code is returned. However, I would like to return a 400 instead. I believe the 404 is being thrown by IIS itself because no matter what I do in the code I can never catch the exception. I have tried the following already:
Implementing an ExceptionHandler: http://www.asp.net/web-api/overview/error-handling/web-api-global-error-handling
Implementing Application_Error: Catching "Maximum request length exceeded"
Implement Application_BeginRequest: http://geekswithblogs.net/sglima/archive/2011/09/20/how-to-handle-maximum-request-length-exceeded-exception.aspx
Surrounding the controller activator in a try catch: http://blog.greatrexpectations.com/2013/05/15/exception-handling-for-web-api-controller-constructors/
Replacing 404.13 error in web.config:
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="13" />
<error statusCode="404" subStatusCode="13" path="/errors/filesize" responseMode="Redirect" />
</httpErrors>
Implementing a DelegatingHandler: Exception Handling ASP.NET MVC Web API
Nothing seems to work and I tried setting break points at the beginning of any of the methods stated above and it never enters any of them when the exception is thrown.