Asp.Net Core上传大文件请求体限制设置
IIS进程内部署时
1. Web.Config的<system.webServer>节点下增加
<security> <requestFiltering> <requestLimits maxAllowedContentLength="20971520" /> </requestFiltering> </security>
2. 配置IISServerOptions选项
services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = 20971520; // 20M
});
若使用IIS托管时,可以根据请求响应的状态码确定是IIS报错(413)还是asp.net core框架(500)报错。
Kestrel部署
1. 配置KestrelServerOptions选项
services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = 20971520; // 20M
});
全局设置(不区分部署方式)
context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = 20971520; // 20M
通过获取IHttpMaxRequestBodySizeFeature接口的实现类来设置最大请求体大小,该接口在不同的部署环境中具体的实现类不一样,
IIS中为:
HTTP1/2 Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT<Microsoft.AspNetCore.Hosting.HostingApplication.Context>
Kestrel中为:
HTTP1 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1Connection<Microsoft.AspNetCore.Hosting.HostingApplication.Context>
HTTP2 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2.Http2Stream<Microsoft.AspNetCore.Hosting.HostingApplication.Context>
这几个实现类中都实现了IHttpMaxRequestBodySizeFeature接口
需要注意的是该设置方式必须在读取请求体之前设置。
internal class Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1ContentLengthMessageBody
protected override void OnReadStarting()
{
long contentLength = this._contentLength;
long? maxRequestBodySize = this._context.MaxRequestBodySize;
if (contentLength > maxRequestBodySize.GetValueOrDefault() & maxRequestBodySize != null)
{
BadHttpRequestException.Throw(RequestRejectionReason.RequestBodyTooLarge);
}
}
最新文章
- UOJ #221 【NOI2016】 循环之美
- javascript-建造者模式
- Java多线程与并发库高级应用-传统线程互斥技术
- CSS 继承深度解析
- 给自定义cell赋值代码
- 中值排序的java实现
- git Please move or remove them before you can merge. 错误解决方案
- log4net资料收集
- mysql中的存储过程和事务隔离
- obj-c 坑
- SQL语法集锦一:SQL语句实现表的横向聚合
- apple
- 通过class实例取得类的接口,父类,构造器
- 架构师之路-在Dubbo中开发REST风格的远程调用
- struts2中配置文件的调用顺序
- IOS中 浅谈iOS中MVVM的架构设计与团队协作
- ionic1 sqlite的添加使用
- [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question
- 页面框架加载完自动执行函数$(function(){});
- js基础-类型转换