c# - Json 对象 Asp.net Core 3.1 的最大长度
问题描述
虽然这是大约 2 年前提出的问题,但我仍然面临这个问题,没有办法摆脱它。有没有办法在 Asp.net Core 3.1 中设置 JSON 对象的最大大小?除了 .Net 核心(或者我可能还没有找到它)之外,在其他 .Net 框架中还有其他方法可以做到这一点。
我有长度约为 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)
{
}
}
解决方案
感谢所有帮助过我的人,我将我的话与解决方案联系起来。我在代码中添加了以下内容并实现了目标。
$.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>
这就是我对代码所做的所有更改,并且我做到了。我希望这段代码可以帮助我们的堆栈团队成员。干杯:)
推荐阅读
- javascript - 搜索过去 [0]
- qt - 如何更改 QML ApplicationWindow Overlay 大小?
- amazon-web-services - 未找到 terraform try() 函数
- javascript - firebase,使用 reactjs 获取经过身份验证的用户
- swift - Swift - 触摸拖动退出以创建形状
- python - 从视频法师 OpenCV Python 中删除特定大小的 blob
- reactjs - 我的反应组件工作正常,但开玩笑测试失败
- javascript - 我可以完全在 Web Service Worker 中运行 Firebase 云消息传递吗?
- javascript - 无法在 Javascript 中访问 window.variable 外部函数
- css - ScrollView 不滚动 react-native