我在 ASP.NET Core 3.1 中实现了一个端点来检索大型 JSON 对象,当此 JSON 开始相对较大时,我遇到了问题。我开始在 5~600KB 左右出现问题。
对于正文低于 600~500KB 的请求,一切正常。
端点定义如下:
[DisableRequestSizeLimit]
[RequestFormLimits(ValueCountLimit = int.MaxValue)]
[HttpPost]
[Route("MyTestEndpoint")]
public void PostTest([FromBody]object objVal)
{
// objVal is null when the post is larger than ~5~600KB
string body = objVal.toString;
....
}
web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.web>
<httpRuntime maxRequestLength="1048576" />
</system.web>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\ApiSLPCalendar.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
<security>
<requestFiltering>
<!-- This will handle requests up to 100MB -->
<requestLimits maxAllowedContentLength="1048576000" />
</requestFiltering>
</security>
</system.webServer>
<system.web.extensions>
<scripting>
<webServices>
<jsonSerialization maxJsonLength="50000000"/>
</webServices>
</scripting>
</system.web.extensions>
</location>
</configuration>
启动.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().ConfigureApiBehaviorOptions(options =>
{
options.SuppressConsumesConstraintForFormFileParameters = true;
options.SuppressInferBindingSourcesForParameters = true;
options.SuppressModelStateInvalidFilter = true;
options.SuppressMapClientErrors = true;
});
services.AddMvc(options =>
{
options.MaxModelBindingCollectionSize = int.MaxValue;
});
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue;
});
....
}
你知道为什么会发生这种情况吗?
******** 编辑********
我尝试了几次更改,但没有一个能正常工作。
最后,根据对 FromBody 字符串参数的讨论给出了 null 我修改了该方法,定义了一个模型,删除了通用对象作为参数。 前端已修改为发送 key="value",其中“Value”表示字符串化的 JSON 对象。
也许您没有达到请求大小限制,但达到了表单大小限制。请尝试
[RequestFormLimits(MultipartBodyLengthLimit = 104857600)]
MultipartBodyLengthLimit是长类型并考虑您的情况。
如果您需要上传文件大小不受限制:-
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue;
});
当请求到达 IIS 时,它将搜索 web.config 以检查最大上传长度。
//clarify code
<!-- 1 GB -->
<requestLimits maxAllowedContentLength="1073741824" />
//clarify code
更新
[HttpPost]
[RequestFormLimits(ValueCountLimit = int.MaxValue)]
[Route("MyTestEndpoint")]
public void PostTest([FromBody]object objVal)
{
....
string body = objVal.toString;
....
}
services.Configure<FormOptions>(options =>
{
options.ValueCountLimit = 10;
options.ValueLengthLimit = int.MaxValue;
options.MultipartBodyLengthLimit = long.MaxValue;
options.MemoryBufferThreshold = Int32.MaxValue;
});
services.AddMvc(options =>
{
options.MaxModelBindingCollectionSize = int.MaxValue;
});
Web.Config
<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>