WebApi 2 Maximum request length exceeded

2019-06-16 21:07发布

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.

2条回答
Fickle 薄情
2楼-- · 2019-06-16 22:08

How it looks like on my server:

<system.web>
<httpRuntime targetFramework="4.6.1" maxRequestLength="16240" />
</system.web>
查看更多
We Are One
3楼-- · 2019-06-16 22:09

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

查看更多
登录 后发表回答