可以作为参数传递给 POST 方法的对象的最大大小 [英] The maximum size of object that can be passed as Parameter to POST method

查看:39
本文介绍了可以作为参数传递给 POST 方法的对象的最大大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 POST 方法的 Web API 控制器,如下所示.

I have a web API controller with a POST method as follows.

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 消耗,如下

This is consumed by a HTTPClient as follows

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)

当控制器的 POST 方法参数中传递的 MyObject 对象大小很小时,它效果很好.但是,如果此对象大小很大,我会在 POST 方法参数中为请求对象获取空值.在一种情况下,从客户端请求传递的 requestClient 对象的大小为 ~5 MB,在 POST 方法中,我将请求对象设为 null.请注意,Web API 托管在 IIS 下.是否需要更改任何设置以获得允许的大小.

It works great when MyObject object size passed in POST method parameter of controller is small in size. However in case when this object size is big, I get a null for the request object in the POST method parameters. In one case, the size of the requestClient object passed from client side request is ~5 MB and in the POST method, I get request object as null. Note that Web API is hosted under IIS. Is there any setting that I need to change for permissible size.

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

UPDATE: Adding following in web.config solved the null object issue in the POST method parameter.

httpRuntime maxRequestLength="2147483647"/>

httpRuntime maxRequestLength="2147483647" />

然后我将 requestClient 对象的大小增加到 ~50MB.现在 POST 方法中的代码永远不会被击中.在客户端,在调用 PostAsJsonAsyn 时,我收到 System.Net.HttpRequestException 并带有以下消息.

I 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.

响应状态码不表示成功:404(未找到).

Response status code does not indicate success: 404 (Not Found).

现在更改 maxRequestLength 似乎没有任何影响.

Now changing maxRequestLength doesn’t seem to have any impact.

推荐答案

来自 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 状态之一,并使用唯一的子状态来标识请求被拒绝的原因:

When request filtering blocks an HTTP request because an HTTP request exceeds the request limits, IIS will return an HTTP 404 error to the client and log one of the following HTTP statuses with a unique substatus that identifies the reason that the request was denied:

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

为解决最大限制问题,应配置请求过滤角色服务(在 (IIS) 7.0 及更高版本中引入的内置安全功能):通过 SERVER MANAGER GUI 或命令实用程序 appcmd.exe 或修改 Web.xml 文件.配置

For Resolving the max limit issue, the Request Filtering role service(built-in security feature that was introduced in (IIS) 7.0 and above) should be configured: by SERVER MANAGER GUI or command utility appcmd.exe or modifying Web.config

    <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/安全/请求过滤

这篇关于可以作为参数传递给 POST 方法的对象的最大大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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