.NET 6 AJAX帖子大阵列导致无效参数
我已经构建了一个.NET 6 MVC应用程序,该应用程序需要以字符串表的形式处理从UI传递的一些数据(实际上是JSON对象,但我尽力简化了它来缩小它的范围)。
这是我的控制器方法:
[HttpPost]
public JsonResult LoadData( List<string> inputData)
{
var success = db.ProcessData(inputData);
return Json(new
{
Success = success
});
}
这是我的JavaScript调用:
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;
}
});
只要我的数组不太大,就会有什么好处(我尝试过一些条目,并且可以使用),但是当我使用Real尝试一下时数据(内容长度为226601,因此没有那么令人印象深刻),我的控制器中的输入数据为null。 并非例外,只是数据被无效。
当我使用iisexpress在VS2022进行调试时,我会遇到这一点。 我尝试通过在项目的根部添加web.config文件来设置一些内容尺寸限制,并且我还尝试在程序中设置MaxrequestBodysize。
有人熟悉这个吗?
编辑: 我只是尝试在传递数组之前在数组上进行json.stringify,然后将参数类型更改为字符串而不是列表。然后,我可以使用jarray.parse(inputData)作为数组解析,并且所有数据似乎都存在。您可以发布的数组的大小是否有限制?
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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于形式数组中的项目数,dotnet core的默认限制为1024。
可以在program.cs文件中设置:
Dotnet core has a default limit of 1024 for the number of items in a form array.
This can be set in the Program.cs file:
您应该尝试将数据作为一个原始对象
,或者如果可能的话,您可以序列化数组两次,并将其作为字符串发送
另一种方式将其发送为搜索,并找到如何将数组大小从1024增加到更多。
you should try to get data as one raw object
or if it is possible you can serialize array twice and send it as a string
another way is to search SO and find how you can increase array size from 1024 to more.