[英]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.