The maximum size of object that can be passed as Parameter to POST method

◇◆丶佛笑我妖孽 提交于 2021-02-08 14:38:12

问题


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);
    }
    ....
}

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)

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.

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

httpRuntime maxRequestLength="2147483647" />

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.

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

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


回答1:


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.

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

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/security/requestfiltering



来源:https://stackoverflow.com/questions/38959442/the-maximum-size-of-object-that-can-be-passed-as-parameter-to-post-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!