简体   繁体   English

如何在 ASP.NET Core 中增加上传大文件的请求长度?

[英]How to increase request length to upload large files in ASP.NET Core?

I have developed an ASP.NET Core Web API which is used to upload files.I am testing the API through postman. I have developed an ASP.NET Core Web API which is used to upload files.I am testing the API through postman. When I try to upload file size more than 28 MB it throws request length exceeded error.当我尝试上传超过 28 MB 的文件时,它会引发超出请求长度的错误。 So I tried to find out solution for this issue.所以我试图找出这个问题的解决方案。 If I increase request length error by changing the value in web.config it works.如果我通过更改 web.config 中的值来增加请求长度错误,它会起作用。 But my requirement is not to use web.config file.但我的要求是不要使用 web.config 文件。 If I increase the MaxRequestBodySize of kestrel server it works fine when I run application through Dotnet CLI but not working when I run application through IIS express from Visual Studio 2019. If I apply [DisableRequestSizeLimit] attribute on POST method it works only on dotnet CLI but not on IIS express.如果我增加红隼服务器的 MaxRequestBodySize 它在我通过 Dotnet CLI 运行应用程序时工作正常,但在我通过 Visual Studio 2019 中的 IIS express 运行应用程序时无法正常工作。如果我在 POST 方法上应用 [DisableRequestSizeLimit] 属性,它仅适用于 dotnet CLI 但不在 IIS 快递上。 How to increase request length globally for IIS express and Dotnet CLI?如何增加 IIS express 和 Dotnet CLI 的全局请求长度? Any help is appreciated.任何帮助表示赞赏。

webBuilder.ConfigureKestrel((context, options) =>
                    {
                       
                        options.Limits.MaxRequestBodySize = 524288000;
                    });

This is the way to configure it globally without changing the web.config:这是在不更改 web.config 的情况下全局配置它的方法:

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .UseKestrel(options =>
    {
        options.Limits.MaxRequestBodySize = 524288000;
    });

if not Kestrel:如果不是红隼:

 .UseHttpSys(options =>
 {
    options.MaxRequestBodySize = 524288000;
 });

OR via middelware通过中间件

        public void Configure(IApplicationBuilder app )
        {
            app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), appBuilder =>
            {
                context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = null;
            });
        }

if not using UseWhen:如果不使用 UseWhen:

app.Run(async context =>
            {
                context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = 524288000;
            });

OR You could also add header to you controller method:或者您也可以添加 header 给您 controller 方法:

  [DisableRequestSizeLimit]
  [HttpPost]
  public async Task<IActionResult> Post([FromBody] List<Obj> obj)
  {
  }

// //

  [RequestSizeLimit(524288000)]
  [DisableRequestSizeLimit]
  [HttpPost]
   public async Task<IActionResult> Post([FromBody] List<Obj> obj)
   {
   }

When hosting your webapplication in IIS/IIS Express, configure the web.config file accordingly.在 IIS/IIS Express 中托管 Web 应用程序时,相应地配置 web.config 文件。 See this answer看到这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 asp.net 内核 2+ 中增加上传大小? 请求的实体太大 - How to increase an upload size in asp.net core 2+? Request entity too large ASP.NET 核心 web 应用 - 如何上传大文件 - ASP.NET Core web application - How to upload large files ASP.NET内核如何上传文件? - How to upload files in ASP.NET Core? 如何通过asp.net中的fileupload控制器上传大文件 - how to upload large files via fileupload controller in asp.net 如何在ASP.NET Core Web API中上传文件 - How to upload files in asp.net core web api 增加 Asp.Net Core 中的上传文件大小 - Increase upload file size in Asp.Net core 如何在 Asp.Net 核心中将上传文件增加到无限大小 - How can Increase upload file to unlimited size in Asp.Net core 如何在 ASP.NET Core 2.2 项目中增加或设置上传媒体文件大小无限制 - How to increase or set the upload media file size unlimited in ASP.NET Core 2.2 project 如何排除 ASP.NET Core 5 中的特定路径,以便请求由 ASP.NET 而不是 static 文件处理? - How to exclude a specific path in ASP.NET Core 5 so that the request is processed by ASP.NET and not by the static files? ASP.Net Core上的上传文件仅在React上返回空的Request.Form.Files - Upload file on ASP.Net Core returns empty Request.Form.Files, only on React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM