![](/img/trans.png)
[英]Attribute Routing Values into Model/FromBody Parameter in ASP.NET CORE
[英]asp.net core webapi frombody parameter size limit
我在 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;
....
}
網絡配置:
<?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;
});
網頁配置
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.