RequestLimitExtension.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using Microsoft.AspNetCore.Builder;
  2. using Microsoft.AspNetCore.Http.Features;
  3. using Microsoft.AspNetCore.Server.Kestrel.Core;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.DependencyInjection;
  6. namespace Extensions
  7. {
  8. public static class RequestLimitExtension
  9. {
  10. /// <summary>
  11. /// 请求body大小设置
  12. /// </summary>
  13. /// <param name="services"></param>
  14. /// <param name="configuration"></param>
  15. public static void AddRequestLimit(this IServiceCollection services, IConfiguration configuration)
  16. {
  17. var sizeM = configuration.GetSection("upload:requestLimitSize").Get<int>();
  18. services.Configure<FormOptions>(x =>
  19. {
  20. x.MultipartBodyLengthLimit = sizeM * 1024 * 1024;
  21. x.MemoryBufferThreshold = sizeM * 1024 * 1024;
  22. x.ValueLengthLimit = int.MaxValue;
  23. });
  24. services.Configure<KestrelServerOptions>(options =>
  25. {
  26. options.Limits.MaxRequestBodySize = sizeM * 1024 * 1024;
  27. });
  28. services.Configure<IISServerOptions>(options =>
  29. {
  30. options.MaxRequestBodySize = sizeM * 1024 * 1024; // 设置最大请求体大小为500MB
  31. });
  32. }
  33. }
  34. }