其他答案已经解决了IIS的限制。然而,从ASP.NET Core 2.0版本开始,Kestrel服务器也会实施自己的默认限制。
KestrelServerLimits.cs的Github页面
如果您想要更改特定MVC操作或控制器的最大请求体大小限制,您可以使用RequestSizeLimit
属性。下面的示例允许MyAction
接受多达100,000,000字节的请求体。
[HttpPost]
[RequestSizeLimit(100_000_000)]
public IActionResult MyAction([FromBody] MyViewModel data)
{
[DisableRequestSizeLimit]
可以用来使请求大小无限制。这有效地恢复了2.0.0之前的行为,只针对所指定的操作或控制器。
如果请求不是由MVC动作处理,仍然可以使用IHttpMaxRequestBodySizeFeature
在每个请求上修改限制。例如:
app.Run(async context =>
{
context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = 100_000_000;
MaxRequestBodySize
是一个可空的长整型。将其设置为 null 将禁用限制,就像 MVC 的 [DisableRequestSizeLimit]
一样。
如果应用程序还没有开始读取请求,则只能在请求之前配置限制;否则会抛出异常。有一个 IsReadOnly
属性,告诉你 MaxRequestBodySize
属性是否处于只读状态,这意味着现在配置限制已经太晚了。
如果您想全局修改最大请求体大小,则可以通过修改 UseKestrel
或 UseHttpSys
的回调函数中的 MaxRequestBodySize
属性来完成。在这两种情况下,MaxRequestBodySize
都是可空的长整型。例如:
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
或者.UseHttpSys(options =>
{
options.MaxRequestBodySize = 100_000_000;
DisableRequestSizeLimit
属性是不够的。我还需要使用 RequestFormLimits
。像这样: [HttpPost("upload"), DisableRequestSizeLimit, RequestFormLimits(MultipartBodyLengthLimit = Int32.MaxValue, ValueLengthLimit = Int32.MaxValue)]
。 - Xav987当您上传任何超过30MB的文件时,您可能会收到404.13 HTTP状态代码。如果您在IIS中运行ASP.Net Core应用程序,则IIS管道将在请求到达应用程序之前拦截您的请求。
请更新您的web.config文件:
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
<!-- Add this section for file size... -->
<security>
<requestFiltering>
<!-- Measured in Bytes -->
<requestLimits maxAllowedContentLength="1073741824" /> <!-- 1 GB-->
</requestFiltering>
</security>
</system.webServer>
之前的ASP.Net应用程序也需要这个部分,但在Core中不再需要,因为您的请求已由中间件处理:
<system.web>
<!-- Measured in kilobytes -->
<httpRuntime maxRequestLength="1048576" />
</system.web>
<configuration>
元素中。 - JackMorrissey也许我来得有点晚,但是这里提供了上传超过30.0 MB文件的完整解决方案,适用于ASP.NET Core版本>=2.0:
您需要执行以下三个步骤:
1. IIS内容长度限制
默认请求限制(maxAllowedContentLength
)为30,000,000字节,大约为28.6 MB。在web.config
文件中自定义限制:
<system.webServer>
<security>
<requestFiltering>
<!-- Handle requests up to 1 GB -->
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
注意:若没有运行该应用程序,IIS将无法工作。
2. ASP.NET Core请求长度限制
对于在IIS上运行的应用程序:
services.Configure<IISServerOptions>(options =>
{
options.MaxRequestBodySize = int.MaxValue;
});
对于在Kestrel上运行的应用程序:
services.Configure<KestrelServerOptions>(options =>
{
options.Limits.MaxRequestBodySize = int.MaxValue; // if don't set default value is: 30 MB
});
3. Form's MultipartBodyLengthLimit
services.Configure<FormOptions>(options =>
{
options.ValueLengthLimit = int.MaxValue;
options.MultipartBodyLengthLimit = int.MaxValue; // if don't set default value is: 128 MB
options.MultipartHeadersLengthLimit = int.MaxValue;
});
添加以上所有选项将解决与上传大小超过30.0 MB的文件相关的问题。
services.Configure<KestrelServerOptions>(options => options.Limits.MaxRequestBodySize = <my value>);
。 - Peheje<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<!-- 1 GB -->
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
在 Startup.cs 文件中,添加以下内容:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue;
x.MultipartHeadersLengthLimit = int.MaxValue;
});
services.AddMvc();
}
在您的 startup.cs
中配置 FormsOptions
Http 功能:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<FormOptions>(o => // currently all set to max, configure it to your needs!
{
o.ValueLengthLimit = int.MaxValue;
o.MultipartBodyLengthLimit = long.MaxValue; // <-- !!! long.MaxValue
o.MultipartBoundaryLengthLimit = int.MaxValue;
o.MultipartHeadersCountLimit = int.MaxValue;
o.MultipartHeadersLengthLimit = int.MaxValue;
});
}
使用IHttpMaxRequestBodySizeFeature
Http 功能来配置MaxRequestBodySize
。public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.Use(async (context, next) =>
{
context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = null; // unlimited I guess
await next.Invoke();
});
}
(Kestrel 是 .NET Core 平台上的一个轻量级 Web 服务器)public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>().UseKestrel(o => o.Limits.MaxRequestBodySize = null);
});
IIS --> web.config:<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<!-- ~ 2GB -->
<httpRuntime maxRequestLength="2147483647" /> // kbytes
</system.web>
<system.webServer>
<security>
<requestFiltering>
<!-- ~ 4GB -->
<requestLimits maxAllowedContentLength="4294967295" /> // bytes
</requestFiltering>
</security>
</system.webServer>
</configuration>
Http.sys:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>().UseHttpSys(options =>
{
options.MaxRequestBodySize = null;
});
});
如果您想上传一个非常大的文件,可能是几GB大小,并且希望将其缓冲到服务器上的`MemoryStream`中,您将收到错误消息 `Stream was too long`,因为`MemoryStream`的容量是`int.MaxValue`。
您需要实现自己的自定义`MemoryStream`类。
但无论如何,缓冲这样大的文件都没有意义。使用 Visual Studio 2022 (v 17.1.6) 和 .net core 6,我在 Program.cs 类中无需更改任何内容。只需要在我的控制器方法中添加这两个属性(除了 [HttpPost] 和 [Route]),就可以在本地运行时接受100MB的上传:
[RequestSizeLimit(100 * 1024 * 1024)]
[RequestFormLimits(MultipartBodyLengthLimit = 100 * 1024 * 1024)]
在我的情况下,我需要仅针对单个页面增加文件上传大小限制。
文件上传大小限制是一项安全功能,关闭或在全局范围内增加它通常不是一个好主意。您不希望某些脚本小子通过上传极大的文件来DOS攻击您的登录页面。文件上传限制可以为此提供一定的保护,因此关闭或在全局范围内增加它并不总是一个好主意。
因此,要仅增加单个页面的限制而非全局性的话:
(我使用ASP.NET MVC Core 3.1和IIS,如果使用Linux则配置会有所不同)
1. 添加web.config
否则,IIS(或者如果在Visual Studio中调试,则是IIS Express)将在请求甚至到达您的代码之前阻止该请求,并显示“HTTP错误413.1 - 请求实体过大”的错误。
请注意“location”标记,它将上传限制限制为特定页面
您还需要“handlers”标记,否则当浏览到该路径时,您将收到HTTP 404错误
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="SomeController/Upload">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<security>
<requestFiltering>
<!--unit is bytes => 500 Mb-->
<requestLimits maxAllowedContentLength="524288000" />
</requestFiltering>
</security>
</system.webServer>
</location>
</configuration>
Next you will need to add the RequestSizeLimit attribute to your controller action, since Kestrel has its own limits too. (you can instead do it via middleware as per other answers if you prefer)
[HttpPost]
[RequestSizeLimit(500 * 1024 * 1024)] //unit is bytes => 500Mb
public IActionResult Upload(SomeViewModel model)
{
//blah blah
}
为了完整性(如果使用MVC),您的视图和视图模型可能如下所示:
视图
<form method="post" enctype="multipart/form-data" asp-controller="SomeController" asp-action="Upload">
<input type="file" name="@Model.File" />
</form>
视图模型
public class SomeViewModel
{
public IFormFile File { get; set; }
}
如果您通过表单提交上传的文件大于128Mb,您也可能会遇到此错误
InvalidDataException: 多部分正文长度限制超过了134217728。
因此,在您的控制器操作中,您可以添加 RequestFormLimits 属性
[HttpPost]
[RequestSizeLimit(500 * 1024 * 1024)] //unit is bytes => 500Mb
[RequestFormLimits(MultipartBodyLengthLimit = 500 * 1024 * 1024)]
public IActionResult Upload(SomeViewModel model)
{
//blah blah
}
使用web.config可能会影响.NET Core的架构,并且在Linux或Mac上部署解决方案时可能会遇到问题。
更好的方法是使用Startup.cs来配置此设置:例如:
services.Configure<FormOptions>(x =>
{
x.ValueLengthLimit = int.MaxValue;
x.MultipartBodyLengthLimit = int.MaxValue; // In case of multipart
});
这里有一个更正:
你还需要添加 web.config 文件,因为当请求到达 IIS 时,它会搜索 web.config 并检查最大上传长度。例如:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<!-- 1 GB -->
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
In your web.config:
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483648" />
</requestFiltering>
</security>
</system.webServer>
Manually edit the ApplicationHost.config file:
ApplicationHost.config
file, locate the <requestLimits>
node.Remove the maxAllowedContentLength
property. Or, add a value that matches the size of the Content-Length header that the client sends as part of the request. By default, the value of the maxAllowedContentLength
property is 30000000.
Save the ApplicationHost.config
file.
为了其他像我这样不幸的人,我将此补充说明:源
在Startup.cs
中:
services.Configure<FormOptions>(options =>
{
options.MultipartBodyLengthLimit = 60000000;
});