WebApi 2 Maximum request length exceeded

Does anyone know if there is a way to catch this error?

In fact, I'm trying to implement some functions that allow the user to upload a file from a web page to the webapi controller.

This works fine, but if the file size exceeds the maximum size specified in the web.config file, the server returns a 404 error.

I want to be able to intercept this and return a 500 error along with a message that can be used by the client.

I can’t decide where to do this in WebApi, because the Application_Error method that I implemented in Global.asax never got in and it seems like IIS is not passing this WebApi application.

+7
asp.net-web-api
source share
1 answer

Try installing IIS to accept requests at 2 GB (in bytes).

<system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="2147483648" /> // 2GB </requestFiltering> </security> </system.webServer> 

And set a reasonable request size for the ASP.NET application (in kilobytes).

 <system.web> <httpRuntime maxRequestLength="4096" /> // 4MB (default) </system.web> 

Now IIS should pass requests less than 2 GB to your application, but the application will go to Application_Error, reach the request size of 4 MB. There you can control what you want to return.

In any case, requests larger than 2 GB will always return IIS 404.13.

Related links:

Work with large files in the ASP.NET web interface

+3
source share

All Articles