请求大小一般在文件上传的时候会用到,当然也防止传过来的参数过大情况。
一、设置请求体的最大值
如果不设置请求体大小默认是 30_000_000 bytes,大约28.6MB,当超出大小时会出现如下错误:
错误:Failed to read the request form. Request body too large. The max request body size is 30000000 bytes.
解决方案:
builder.WebHost.ConfigureKestrel((context, options) => { //设置最大1G, 这里的单位是byte options.Limits.MaxRequestBodySize = 1073741824; });
如果传参用的不是表单的形式(如文件上传)这样处理是足够了的,如果是则还需要设置表单的最大长度。
二、设置表单的最大值
如果不设置表单长度默认是 134,217,728 bytes,大约128MB,当超出大小时会出现如下错误:
错误:Failed to read the request form. Multipart body length limit 134217728 exceeded.
解决方案:
builder.Services.Configure<FormOptions>(option => { //设置最大1G, 这里的单位是byte option.MultipartBodyLengthLimit = 1073741824; });
三、IIS下的配置
如果是挂在IIS下,还需如下操作:
- 修改C:\Windows\System32\inetsrv\config\schema\IIS_schema.xml中maxAllowedContentLength的大小;
- 修改项目web.config配置system.webServer/serverRuntime/maxRequestEntityAllowed的大小;
- 修改项目web.config配置system.web/httpRuntime/maxRequestLength的大小;
- 重启IIS。
文章转载于:https://www.helloworld.net/p/9291788101
下面是github的issue,基本上可以解决大部分请求遇到的大小限制问题,可根据自己需要添加相关代码:
https://github.com/dotnet/aspnetcore/issues/20369
1. IIS content length limit
The default request limit (maxAllowedContentLength) is 30,000,000 bytes, which is approximately 28.6MB. Customize the limit in the web.config file:
<system.webServer>
<security>
<requestFiltering>
<!-- Handle requests up to 1 GB -->
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
Note: Without this application running on IIS would not work.
2. ASP.NET Core Request length limit:
For application running on IIS:
services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = int.MaxValue;
});
For application running on Kestrel:
services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = int.MaxValue; // if don't set default value is: 30 MB
});
3. Form's MultipartBodyLengthLimit
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue; // if don't set default value is: 128 MB
x.MultipartHeadersLengthLimit = int.MaxValue;
});
Adding all the above options will solve the problem related to the file upload with size more than 30.0 MB.
标签:Core,ASP,IIS,MB,int,MaxValue,limit,NET,options From: https://www.cnblogs.com/riddly/p/17467420.html