11

Does anyone know if there is a way of catching this error?

Essentially I'm trying to implement some functionality to allow a user to upload a file from a webpage to a webapi controller.

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

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

I can't work out where to do this in WebApi as the Application_Error method I've implemented in Global.asax is never hit and it seems like IIS is not passing this through to the WebApi application.

llihp
  • 195
  • 1
  • 2
  • 10
  • 1
    You need to increase the maximum request size allowed. See this answer: http://stackoverflow.com/questions/288612/how-to-increase-the-max-upload-file-size-in-asp-net – Rosdi Kasim Jul 15 '15 at 17:07
  • 2
    I have done already done this, but I want to be able to catch the error in instances when this has been exceeded. – llihp Jul 15 '15 at 18:00

2 Answers2

14

Try to set IIS to accept 2GB requests(in Bytes).

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

And set reasonable request size for ASP.NET app(in kiloBytes).

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

Now IIS should let requests less than 2GB pass to your app, but app will jump into Application_Error reaching 4MB request size. There you can manage what you want to return.

Anyway, requests greater than 2GB will always return 404.13 by IIS.

Related links:

Dealing with large files in ASP.NET Web API

dropoutcoder
  • 2,627
  • 2
  • 14
  • 32
  • 2
    This post has some additional details on the above configuration: https://stackoverflow.com/a/40085473/152648 – Grinn Jul 26 '19 at 19:22
3

How it looks like on my server:

<system.web>
<httpRuntime targetFramework="4.6.1" maxRequestLength="16240" />
</system.web>
kurtanamo
  • 1,808
  • 22
  • 27