Search code examples
ajaxasp.net-mvc

.net 6 Ajax POST Large array results in null parameter


I have built a .net 6 MVC Application which needs to process some data passed from the UI in the form of a table of strings (actually JSON objects but I tried simplifying as much as I could to narrow it down).

Here's my Controller Method:

[HttpPost]
public JsonResult LoadData( List<string> inputData)
{
    var success = db.ProcessData(inputData);

    return Json(new
    {
        Success = success
    });

}

And here is my Javascript call:

var inputData = [];

$.each(tableData.body, function (index, item) {
    inputData.push(JSON.stringify(jQuery.parseJSON(item[0])));
});

var data = {
    inputData: inputData
}

$.ajax({
    type: "POST",
    url: '@Url.Action("LoadData", "myController")',
    data: data,
    success: function(response) {
                                
    },
    failure: function(response) {
        debugger;
    },
    error: function(response) {
        debugger;
    }
});

What happens is that it all works fine as long as my array is not too big (I've tried with a few entries and it works), but when I try it with real data (content length is 226601, so not that impressive), the inputData in my controller is null. There is not exception, just the data is nulled.

I encounter this when I debug it in VS2022 with IISExpress. I tried setting some content size limit by adding a web.config file at the root of my project, and I also tried setting MaxRequestBodySize in my Program.cs, but so far nothing has worked.

Is anyone familiar with this ?

EDIT: I just tried to do JSON.Stringify on my array before passing it, and changing the parameter type to string instead of List. Then I'm able to parse it as an array using JArray.Parse(inputData), and all the data seems to be there. Is there a limit to the size of an array you can post ?


Solution

  • you should try to get data as one raw object

    [HttpPost]
    public JsonResult LoadData( JArray inputData)
    {
    var data=inputData.ToObject<List<string>>();
    }
    

    or if it is possible you can serialize array twice and send it as a string

    [HttpPost]
    public JsonResult LoadData( string inputData)
    {
       var json = JsonConvert.DeserializeObject<string>(inputData);
    }
    

    another way is to search SO and find how you can increase array size from 1024 to more.