【发布时间】:2021-09-05 01:48:21
【问题描述】:
我在 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>
Startup.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 string parameter is giving null 的讨论之后,我修改了方法,定义了一个模型,删除了泛型对象,作为参数。 前端已经修改为发送一个key="value",其中"Value"代表一个字符串化的JSON对象。
【问题讨论】:
标签: c# asp.net-core asp.net-web-api