在Asp.Net Core中增加上传文件的大小
当前,我正在使用Asp.Net Core,MVC6需要上传的文件大小不受限制.我已经搜索了它的解决方案,但仍然没有得到实际的答案.
Currently, I am working with Asp.Net Core and MVC6 need to upload file size unlimited. I have searched its solution but still not getting the actual answer.
如果有人有任何想法,请帮助.
If anyone have any idea please help.
谢谢.
其他答案解决了IIS限制.但是,从 ASP.NET Core 2.0开始,Kestrel服务器也施加了自己的默认限制.
The other answers solve the IIS restriction. However, as of ASP.NET Core 2.0, Kestrel server also imposes its own default limits.
如果要更改特定MVC操作或控制器的最大请求正文大小限制,则可以使用RequestSizeLimit
属性.以下内容将允许MyAction
接受最大100,000,000字节的请求正文.
If you want to change the max request body size limit for a specific MVC action or controller, you can use the RequestSizeLimit
attribute. The following would allow MyAction
to accept request bodies up to 100,000,000 bytes.
[HttpPost]
[RequestSizeLimit(100_000_000)]
public IActionResult MyAction([FromBody] MyViewModel data)
{
[DisableRequestSizeLimit]
可用于使请求大小不受限制.这样可以有效地还原仅归因于操作或控制器的2.0.0之前的行为.
[DisableRequestSizeLimit]
can be used to make request size unlimited. This effectively restores pre-2.0.0 behavior for just the attributed action or controller.
如果MVC操作未处理该请求,则仍可以使用IHttpMaxRequestBodySizeFeature
在每个请求的基础上修改限制.例如:
If the request is not being handled by an MVC action, the limit can still be modified on a per request basis using the IHttpMaxRequestBodySizeFeature
. For example:
app.Run(async context =>
{
context.Features.Get<IHttpMaxRequestBodySizeFeature>().MaxRequestBodySize = 100_000_000;
MaxRequestBodySize
是可为空的long.将其设置为null将禁用MVC [DisableRequestSizeLimit]
之类的限制.
MaxRequestBodySize
is a nullable long. Setting it to null disables the limit like MVC's [DisableRequestSizeLimit]
.
仅当应用程序尚未开始读取时,您才可以配置请求的限制;否则将引发异常.有一个IsReadOnly
属性,它会告诉您MaxRequestBodySize
属性是否处于只读状态,这意味着配置该限制为时已晚.
You can only configure the limit on a request if the application hasn’t started reading yet; otherwise an exception is thrown. There’s an IsReadOnly
property that tells you if the MaxRequestBodySize
property is in read-only state, meaning it’s too late to configure the limit.
如果要全局修改最大请求正文大小,可以通过在UseKestrel
或UseHttpSys
的回调中修改MaxRequestBodySize
属性来完成.在两种情况下,MaxRequestBodySize
都是可为空的long.例如:
If you want to modify the max request body size globally, this can be done by modifying a MaxRequestBodySize
property in the callback of either UseKestrel
or UseHttpSys
. MaxRequestBodySize
is a nullable long in both cases. For example:
.UseKestrel(options =>
{
options.Limits.MaxRequestBodySize = null;
或
.UseHttpSys(options =>
{
options.MaxRequestBodySize = 100_000_000;