1

Although this is queried around 2 years ago, but I'm still facing the issue and there is no way to get out of it without. Is there any way to set Max size for JSON object in Asp.net Core 3.1? There are ways to do this in other .Net frameworks except .Net core (or may I ain't found it yet).

How to Set Max Json Length in .net core

I'm having JSON object array of length around 3k, same file with around 20 objects works fine but as I increase the size the data goes null in controller method (Even while inspecting in DOM, console.log show the exact array of object).

console.log(candidate_List);
        $.ajax({
                     
            url: '@Url.Action("Insert_Candidates")',
            data: { "selected_list": candidate_List },
            type: "POST",
            dataType: "json",
            async: true,

            success: function (response) {
                alert(response.d);
            },
            failure: function (response) {
                alert("fail");
            }
        });
       [HttpPost]
        public async Task<IActionResult> Insert_Candidates([FromForm]List<CandidateDTO> selected_list)
        {
            string result = "";
            if (ModelState.IsValid)
            {
                
            }
        }
Rameez Javed
  • 139
  • 3
  • 9
  • Have you tried anything suggested here: [maximum-http-request-size-for-asp-web-api-with-json](https://stackoverflow.com/questions/38563118/maximum-http-request-size-for-asp-web-api-with-json) ? – Ryan Wilson Apr 02 '21 at 20:11
  • I've used this is MVC and Asp.net webforms but here I don't have webconfig file in .net core project where I can add the MaxRequestLength. (If you have the knowledge let me know I'll try that way) – Rameez Javed Apr 02 '21 at 20:22
  • Why [`[FromForm]`](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.fromformattribute?view=aspnetcore-5.0)? Are you sending `application/json` JSON in the body, or sending `application/x-www-url-formencoded` form data in the body? See: [differences in application/json and application/x-www-form-urlencoded](https://stackoverflow.com/q/9870523/3744182) and also [Jquery Ajax Posting JSON to webservice](https://stackoverflow.com/q/6323338/3744182). – dbc Apr 02 '21 at 20:54
  • @RameezJaved When deploying an Asp.Net Core web app, the web.config is auto-generated, you can add one to your solution and it will still be honored when publishing to the server. – Ryan Wilson Apr 03 '21 at 03:19
  • @dbc I'm sending json data, – Rameez Javed Apr 03 '21 at 13:39
  • In that case, you might try some of the solutions from [Increase upload file size in Asp.Net core](https://stackoverflow.com/q/38698350) or (since you are using `[FromForm]`) [C# WebRequest Send Form Data Size Limit](https://stackoverflow.com/q/57073940). – dbc Apr 03 '21 at 20:13
  • @dbc thankyou, added FromForm code its show internal server error 500, I've tried 311 rows now its inserting fine. It means somewhere more size should be increase to insert rows around 3k? – Rameez Javed Apr 04 '21 at 14:40
  • Thanks everyone, I've achieved it, #Stackoverflow has something awesome in it, Its team never lets anyone in bugs, @dbc followed your instruction and added some code in startup. putting my answer, open to vote. – Rameez Javed Apr 04 '21 at 17:45

1 Answers1

5

Thanks to all those who helped me, I'm putting my words with solution. I've added the following thing in my code and achieved the target.

$.ajax({
            url: '@Url.Action("Insert_Candidates")',
            data: { "selected_list": candidate_List},
            type: "POST",
            dataType: "json",
            async: true,

            success: function (response) {
                alert(response.d);
            },
            failure: function (response) {
                alert("fail");
            }
        });


Controller's Method here


        [HttpPost]
        [RequestFormLimits(ValueCountLimit = int.MaxValue)]
        public async Task<IActionResult> Insert_Candidates([FromForm]List<RecuritementDTO> selected_list)
        {
            string result = "";
            if (ModelState.IsValid)
            {

                try
                {
                    result = count + "  rows has been saved";
                }
                catch (Exception ex)
                {

                    //throw;
                    string message = ex.ToString();
                    result = ex.Message.ToString();
                }
            }
           
            return Json(new { d = result });
        }

Startus.cs here

services.Configure<FormOptions>(options =>
            {
                options.ValueCountLimit = 10; //default 1024
                options.ValueLengthLimit = int.MaxValue; //not recommended value
                options.MultipartBodyLengthLimit = long.MaxValue; //not recommended value
                options.MemoryBufferThreshold = Int32.MaxValue;
            });
            services.AddMvc(options =>
            {
                options.MaxModelBindingCollectionSize = int.MaxValue;
            });

Here goes Webconfig code (I've added this too... may be it helped me to cross the bridge)

<system.web>
    <httpRuntime  maxRequestLength="1048576" />  
</system.web>
    <system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="2147483648" />
    </requestFiltering>
  </security>
</system.webServer>
<system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
   </system.web.extensions>

This is all I've made change to my code and I've made it. I hope this code might help our stack team members. Cheers :)

Rameez Javed
  • 139
  • 3
  • 9