猿问

AJAX POST 数据到 ASP.NET Core 中的 MVC 控制器在传递超过一定长度的对

我有一个 javascript 函数,它正在创建一个包含对象数组的模型,并使用 AJAX POST 请求将该数据发送到我的 MVC 控制器。出于某种原因,似乎可以传递给控制器的数组的大小有限制,尽管经过实验,在我的例子中似乎在 450 到 500 个对象之间。一旦它大于该阈值,控制器将只接收一个空值。是否可以通过一些配置或解决方法来消除或解决此限制?


我已经尝试了很多基于 web.config 的“解决方案”,很多人就类似的 SO 问题提出这些解决方案都无济于事(我认为这些可能仅适用于 .Net Framework 的 ASP.NET 版本,不适用于 .NET Core) .


Java脚本:


var i = 0;

$("#SupplierNumber option").each(function () {

    var supplierNumber = {};

    supplierNumber.Value = $(this).val();

    supplierNumber.text = $(this).val();

    supplierNumbers.push(supplierNumber);

    //Below limits max size for testing

    i = i + 1;

    if (i > 450) {

        return false;

    }

});


//Other non-array values gathered here

var model = {

    "SupplierNumbers": supplierNumbers,

    //... other non-array values added to model

}


$.ajax({

    type: "POST",

    url: "/Administration/TestPost",

    data: model,

    dataType: "json",

    success: function (data) {

        alert("Finished");

    }

});

控制器:


[HttpPost]

public async Task<IActionResult> TestPost(TestViewModel model)

{

    //model is null here when array is > 500 in size 

    return Json(new { success = true });

}


牧羊人nacy
浏览 284回答 5
5回答

qq_花开花谢_0

在使用 JSON.stringify 之前试试这个!我有完全相同的问题。我需要做的就是以下几点:在我的 Startup.cs 中,我在ConfigureServices中添加了以下几行:services.Configure<FormOptions>(options&nbsp;=> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options.ValueCountLimit&nbsp;=&nbsp;int.MaxValue; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;options.ValueLengthLimit&nbsp;=&nbsp;int.MaxValue; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});

慕的地8271018

我不知道确切的问题是什么,但您可以尝试此解决方案。在将字符串反序列化为对象之后,尝试将数据作为刺痛传递到操作中。var model = {"SupplierNumbers": supplierNumbers,//... other non-array values added to model}$.ajax({type: "POST",url: "/Administration/TestPost",data: {"data":JSON.stringify(model)},dataType: "json",success: function (data) {&nbsp; &nbsp; alert("Finished");}});在控制器端,您可以将此字符串反序列化以进行建模。[HttpPost]public async Task<IActionResult> TestPost(string data){&nbsp;TestViewModel model=DeserializeObject<TestViewModel>(data);return Json(new { success = true });}

慕哥6287543

我们需要设置值计数限制和长度。请在 startup.cs 中添加以下代码 --- 它对我有用&nbsp; &nbsp; &nbsp; &nbsp; services.AddMvc(options =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options.MaxModelBindingCollectionSize = 100000;&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; services.Configure<FormOptions>(options =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options.ValueCountLimit = int.MaxValue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options.ValueLengthLimit = int.MaxValue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; options.MultipartHeadersLengthLimit = int.MaxValue;&nbsp; &nbsp; &nbsp; &nbsp; });

ITMISS

这里有两个解决方案:首先demo,使用json类型的数据,ajax中我们通常使用这种方式传递array(传递json类型的数据,需要contentType: 'application/json'在ajax中使用,在action中使用[FromBody]):索引视图模型:public class IndexViewModel    {        public int Id { get; set; }        public string Name { get; set; }    }控制器:        [HttpGet]        public IActionResult TestAjax() {            return View();        }        [HttpPost]        public IActionResult TestAjaxWithJsonData([FromBody]List<IndexViewModel> l) {            return Ok();        }看法: <button onclick="postdata1()">submit(jsondata)</button>        @section scripts{     <script type="text/javascript">        function postdata1() {            var a = [];            for (var i = 0; i < 500; i++) {                var indexViewModel = {};                indexViewModel.Id = i;                indexViewModel.Name = "name" + i;                a.push(indexViewModel);            }            var data = JSON.stringify(a);            $.ajax({                type: "POST",                url: 'TestAjaxWithJsonData',                data: data,                contentType: 'application/json'            }).done(function (data) {            });        }    </script>}第二个演示,使用 FormData:控制器:[HttpPost]    public IActionResult TestAjaxWithFormdata(List<IndexViewModel> l)    {        return Ok();    }看法:<button onclick="postdata2()">submit(formdata)</button>        @section scripts{     <script type="text/javascript">                function postdata2() {            var formdata = new FormData();            for (var i = 0; i < 500; i++) {                formdata.append("l[" + i + "].Id", i);                formdata.append("l[" + i + "].Name", "name" + i);            }        $.ajax({                type: "POST",                url: 'TestAjaxWithFormdata',                cache: false,                contentType: false,                processData: false,                data: formdata,             }).done(function (data) {        });        }            </script>}

大话西游666

经过更多搜索后,我发现这是 ASP.NET Core 中的配置问题。默认情况下,复杂模型中可以绑定的最大值数为 1024。在 Startup 类的 ConfigureServices 方法中添加以下选项将允许您将此限制增加到您需要的任何值 。services.AddMvc(options =>{    options.MaxModelBindingCollectionSize = 100000; })另外,我发现人们在面对这个问题时可能会发现有用的其他东西,但是使用表单提交操作,是下面的选项,它采用与上面提到的相同的方法(source)。services.Configure<FormOptions>(options => options.ValueCountLimit = 100000);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答