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);
        }
}

最新文章

  1. UOJ #221 【NOI2016】 循环之美
  2. javascript-建造者模式
  3. Java多线程与并发库高级应用-传统线程互斥技术
  4. CSS 继承深度解析
  5. 给自定义cell赋值代码
  6. 中值排序的java实现
  7. git Please move or remove them before you can merge. 错误解决方案
  8. log4net资料收集
  9. mysql中的存储过程和事务隔离
  10. obj-c 坑
  11. SQL语法集锦一:SQL语句实现表的横向聚合
  12. apple
  13. 通过class实例取得类的接口,父类,构造器
  14. 架构师之路-在Dubbo中开发REST风格的远程调用
  15. struts2中配置文件的调用顺序
  16. IOS中 浅谈iOS中MVVM的架构设计与团队协作
  17. ionic1 sqlite的添加使用
  18. [leetcode] 329. Longest Increasing Path in a Matrix My Submissions Question
  19. 页面框架加载完自动执行函数$(function(){});
  20. js基础-类型转换

热门文章

  1. Spring Boot 防止接口被恶意刷新、暴力请求
  2. ORACLE数据库起不来
  3. Cesium点击改变entity/primitives颜色与恢复原色(三)
  4. Vue学习笔记之表单绑定
  5. 一文搞懂│http 和 https 的通信过程及区别
  6. 从0搭建Vue3组件库(二):Monorepo项目搭建
  7. Vitis-AI之docker指南
  8. Vue props配置项(属性)
  9. [C#]为debug添加DebuggerDisplay属性
  10. accessservice对于难定位的view如何定位