WebApi 2超出最大请求长度 [英] WebApi 2 Maximum request length exceeded

查看:129
本文介绍了WebApi 2超出最大请求长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道是否有捕获此错误的方法吗?

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

基本上,我正在尝试实现一些功能,以允许用户将文件从网页上传到webapi控制器.

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

这可以正常工作,但是如果文件大小超过了web.config中指定的最大大小,则服务器将返回404错误.

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

我希望能够拦截此错误并返回500错误以及客户端可以使用的消息.

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

由于在Global.asax中实现的Application_Error方法从未被使用过,而且似乎IIS并未将其传递给WebApi应用程序,因此我无法在WebApi的何处执行此操作.

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.

推荐答案

尝试将IIS设置为接受2GB请求(以字节为单位).

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

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

并为ASP.NET应用设置合理的请求大小(以千字节为单位).

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

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

现在IIS应该允许将小于2GB的请求传递到您的应用程序,但是应用程序将跳入Application_Error,达到4MB的请求大小.在那里,您可以管理要返回的内容.

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.

无论如何,大于2GB的请求将始终由IIS返回404.13.

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

相关链接:

大型交易ASP.NET Web API中的文件

这篇关于WebApi 2超出最大请求长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆