> 文章列表 > IIS .Net Core 413错误和Request body too large解决办法

IIS .Net Core 413错误和Request body too large解决办法

IIS .Net Core 413错误和Request body too large解决办法

错误描述

图片比较大时,在前端上传就报413错误。根本到不了后端。

在网上看到这个文章比较有用。

https://blog.csdn.net/wstever/article/details/128870742

1、修改网站Web.config配置文件

加入下面这段配置

<?xmlversion="1.0" encoding="utf-8"?>

<configuration>

<location path="." inheritInChildApplications="false">

<system.webServer>

<handlers>

<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />

</handlers>

<aspNetCore processPath="dotnet" arguments=".\\WebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\\logs\\stdout" hostingModel="inprocess" />

<security>

<requestFiltering>

<requestLimits maxAllowedContentLength="209715200" />

</requestFiltering>

</security>

</system.webServer>

</location>

</configuration>

经过上面配置,本地上传成功,但是其他机器上报 Requestbody too large

Request body too large 错误解决方法

1、修改Startup.cs

public void ConfigureServices(IServiceCollection services)

{

services.AddFxServices();

services.AddAutoMapper();

//解决文件上传Request body too large

services.Configure<FormOptions>(x=>

{

x.MultipartBodyLengthLimit = 209_715_200;//最大200M

});

}

2、修改接口方法

加上 [DisableRequestSizeLimit]

IIS .Net Core 413错误和Request body too large解决办法

这个时候将项目重新发布部署一下,低于200M的文件就可以正常上传了。

注意:上面的解决方法只适用于将.Net Core项目部署在IIS下。

如果是部署Linux系统下(参考其他博主的解决方法,具体没有进行测试论证,仅供参考)

需要在 Program.cs 添加如下代码

public static IWebHost BuildWebhost(string[] args) =>

WebHost.CreateDefaultBuilder(args)

.UseStartup<Startup>()

.UseKestrel(options=> {

options.Limits.MaxRequestBodySize = 209715200; // 200M

})

.Build();