net core Web api项目
我有一个帖子请求方法,该方法必须适用于许多实体
如果我使用发送20.000或30.000实体,则它不是问题方法,但是如果我发送的数据太大,例如100.000 80.000甚至没有落入方法,并且找不到返回给客户的
我该怎么办?
这是我的方法
[HttpPost]
[DisableRequestSizeLimit]
public async Task<IActionResult> ArchivePlugs([FromBody] ArchivePlugs model)
{
try
{
await _archivePlugsRepository.AddSynchronizeAsync(model.Archive_Plugs);
await _archivePlugProductRepository.AddSynchronizeAsync(model.Archive_PlugProduct);
await _archivePlugTaxsRepository.AddSynchronizeAsync(model.Archive_PlugTax);*/
return OkResult();
}
catch
{
return BadRequestResult();
}
}
我尝试了什么?
[DisableRequestSizeLimit]
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.ContractResolver = CustomContractResolver.Instance;
});
并删除[frombody]标签并在此方法中使用
using (var reader = new StreamReader(Request.Body))
{
var body = reader.ReadToEnd();
ArchivePlugs results = JsonConvert.DeserializeObject<ArchivePlugs>(body);
}
但它们都不起作用
答案 0 :(得分:0)
如果使用的是IIS服务器,则需要在web.config文件中设置requestLimits:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<!-- 4294967295 bytes (4 GiB) is the maximum value-->
<requestLimits maxAllowedContentLength="52428800" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
您还可以在StartUp类中设置FormOptions:
public void ConfigureServices(IServiceCollection services)
{
// Set limits for form options, to accept big data
services.Configure<FormOptions>(x =>
{
x.BufferBody = false;
x.KeyLengthLimit = 2048; // 2 KiB
x.ValueLengthLimit = 4194304; // 32 MiB
x.ValueCountLimit = 2048;// 1024
x.MultipartHeadersCountLimit = 32; // 16
x.MultipartHeadersLengthLimit = 32768; // 16384
x.MultipartBoundaryLengthLimit = 256; // 128
x.MultipartBodyLengthLimit = 134217728; // 128 MiB
});
// Add the mvc framework
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
...
} // End of the ConfigureServices method
另一个选择是发送较小的ArchivePlug。