首页 > 解决方案 > Json 对象 Asp.net Core 3.1 的最大长度

问题描述

虽然这是大约 2 年前提出的问题,但我仍然面临这个问题,没有办法摆脱它。有没有办法在 Asp.net Core 3.1 中设置 JSON 对象的最大大小?除了 .Net 核心(或者我可能还没有找到它)之外,在其他 .Net 框架中还有其他方法可以做到这一点。

如何在 .net core 中设置最大 Json 长度

我有长度约为 3k 的 JSON 对象数组,具有大约 20 个对象的同一文件工作正常,但随着我增加大小,控制器方法中的数据变为空(即使在 DOM 中检查时,console.log 显示对象的确切数组) .

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)
            {
                
            }
        }

标签: c#asp.netjsonajaxasp.net-core

解决方案


感谢所有帮助过我的人,我将我的话与解决方案联系起来。我在代码中添加了以下内容并实现了目标。

$.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]
        [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 在这里

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;
            });

这是Webconfig代码(我也添加了这个......可能是它帮助我过桥)

<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>

这就是我对代码所做的所有更改,并且我做到了。我希望这段代码可以帮助我们的堆栈团队成员。干杯:)


推荐阅读