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

查看:97
本文介绍了可以作为参数传递给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方法参数中我将获得请求对象的null.在一种情况下,从客户端请求传递的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.配置

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>

有关配置的详细信息,请查看:

For Configuration details review :

https://www.iis.net/configreference/system.webserver/安全/请求过滤

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

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