Search code examples
jsonapiasp.net-core-webapihttpconnection

StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent,


{StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Cache-Control: private
  Server: Microsoft-IIS/10.0
  X-Powered-By: ASP.NET
  Date: Sat, 06 Jun 2020 02:26:17 GMT
  Connection: close
  Content-Type: text/html; charset=utf-8
  Content-Length: 5059
}}

when calling a post method in api producing the response message without hitting the api,we are using stringcontent to pass input parameters as a serialized json which has 200k objects. our basic understanding is that its because content exceeds limited length. we are using .net core 3.1. how to increase the length of maximum allowed? we already tried placing [RequestSizeLimit(long.MaxValue)] on controller level. our web api call

 [Route("~/api/Controller/somemethod")]
 [HttpPost]
public string somemethod(List<DataMaster> Lists)

our api calling method

url = "https://localhost:44339/" + url;
            HttpContent httpContent = new StringContent(inputParams, Encoding.UTF8, "application/json");
            HttpRequestMessage request = new HttpRequestMessage
            {
                Method = new HttpMethod(method),
                RequestUri = new Uri(url),
                Content = httpContent
            };
            HttpClientHandler clientHandler = new HttpClientHandler();

            clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => { return true; };

            HttpClient client = new HttpClient(clientHandler);

            return client.SendAsync(request).Result;

Solution

  • You can set the MaxRequestBodySize (docs):

    Gets or sets the maximum allowed size of any request body in bytes. When set to null, the maximum request length will not be restricted in ASP.NET Core. However, the IIS maxAllowedContentLength will still restrict content length requests (30,000,000 by default). This limit has no effect on upgraded connections which are always unlimited. This can be overridden per-request via IHttpMaxRequestBodySizeFeature.

    services.Configure<IISServerOptions>(options =>
    {
        options.MaxRequestBodySize = 2147483647;
    });