可以作为参数传递给POST方法的对象的最大大小

本文关键字:对象 方法 POST 参数传递 | 更新日期: 2023-09-27 18:02:24

我有一个web API控制器与POST方法如下。

public class MyController : ApiController
{
    // POST: api/Scoring
    public HttpResponseMessage Post([FromBody]MyClass request)
    {
        // some processing of request object
        return Request.CreateResponse(HttpStatusCode.OK, someResponseObject);
    }
    ....
}

HTTPClient按照如下方式使用

HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.BaseAddress = new Uri("http://localhost");
MyClass requestClient = new MyClass();
var task = httpClient.PostAsJsonAsync("api/my", requestClient)

当MyObject对象的大小传递在POST方法参数的控制器是小的尺寸时,它工作得很好。但是,如果这个对象的大小很大,我将在POST方法参数中获得请求对象的空值。在一种情况下,从客户端请求传递的requestClient对象的大小约为5 MB,并且在POST方法中,我将请求对象获取为空。请注意,Web API托管在IIS下。我是否需要更改允许的大小。

更新:在网页中添加以下内容。config解决了POST方法参数中的空对象问题。

httpRuntime maxRequestLength="2147483647"/>

然后我将requestClient object的大小增加到~50MB。现在POST方法中的代码永远不会被击中。在客户端,在调用postasjsonasynn,我得到System.Net.HttpRequestException与以下消息。

响应状态码不显示成功:404 (not Found).

现在改变maxRequestLength似乎没有任何影响。

可以作为参数传递给POST方法的对象的最大大小

From OP:

> then increased the size of requestClient object to ~50MB. Now the code in POST method never getting hit. On the client side, at the call to PostAsJsonAsyn, I get System.Net.HttpRequestException with following message.
Response status code does not indicate success: 404 (Not Found).
Now changing maxRequestLength doesn’t seem to have any impact.

当请求过滤因为HTTP请求超出请求限制而阻止HTTP请求时,IIS将向客户端返回HTTP 404错误,并记录以下HTTP状态之一,并使用唯一的子状态标识请求被拒绝的原因:

 HTTP Substatus      Description
 404.13                 Content Length Too Large
 404.14                 URL Too Long
 404.15                 Query String Too Long
 ..etc

要解决最大限制问题,应通过SERVER MANAGER GUI或命令实用程序appcmd.exe或修改Web.config

配置请求过滤角色服务(在(IIS) 7.0及更高版本中引入的内置安全功能)。
    <configuration>
       <system.webServer>
          <security>
             <requestFiltering>
                 .....
                <!-- limit post size to 10mb, query string to 256 chars, url to 1024 chars -->
                <requestLimits maxQueryString="256" maxUrl="1024" maxAllowedContentLength="102400000" />
                .....
             </requestFiltering>
          </security>
       </system.webServer>
    </configuration>

查看配置细节:

https://www.iis.net/configreference/system.webserver/security/requestfiltering