ASP.NET Core 2.2 升级 - IIS:限制请求内容长度
ASP.NET Core 2.2 Upgrade - IIS: limit request content length
我们以前 运行 在 ASP.NET Core 1.1 上提供我们的服务,直到现在。我们刚刚升级到 ASP.NET Core 2.2,非常顺利。
但是,我们在 Windows 上托管在 Azure App Service 上,这又似乎是 运行 IIS。
现在我们在 web.config
中有一个自定义部分来限制最大内容长度,因此当用户上传文件时,他们在实际上传到限制之前知道如果上传失败:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="api/0.1/files">
<system.web>
<httpRuntime maxRequestLength="409600" executionTimeout="3600"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="419430400" />
</requestFiltering>
</security>
</system.webServer>
</location>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore"
path="*"
verb="*"
modules="AspNetCoreModule"
resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="dotnet"
arguments=".\webapp.dll"
stdoutLogEnabled="false"
stdoutLogFile="\?\%home%\LogFiles\stdout"
hostingModel="OutOfProcess"
forwardWindowsAuthToken="false"/>
</system.webServer>
</location>
</configuration>
现在,调用路由 api/0.1/files
(当然还有所有路由 "beneath" 文件)将产生 404 not found 结果并显示以下错误消息:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
我能找到的唯一解决方法是全局限制内容长度:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore"
path="*"
verb="*"
modules="AspNetCoreModule"
resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="dotnet"
arguments=".\webapp.dll"
stdoutLogEnabled="false"
stdoutLogFile="\?\%home%\LogFiles\stdout"
hostingModel="OutOfProcess"
forwardWindowsAuthToken="false"/>
<security>
<requestFiltering>
<!--400 MByte-->
<requestLimits maxAllowedContentLength="419430400" />
</requestFiltering>
</security>
</system.webServer>
</location>
</configuration>
为特定路线设置限制的正确方法是什么?
可以使用location为特定路线设置限制。
此外,如果您想更改特定 MVC 操作或控制器的最大请求正文大小限制,您可以使用 RequestSizeLimit
属性。
// GET api/values/5
[HttpGet("{id}")]
[RequestSizeLimit(40000000)]
public ActionResult<string> Get(int id)
{
return "value";
}
它设置此操作方法允许的最大请求长度。您可以在操作级别或控制器级别应用此属性。这是在 ASP.NET 核心 MVC 应用程序中增加限制的推荐方法。
我们以前 运行 在 ASP.NET Core 1.1 上提供我们的服务,直到现在。我们刚刚升级到 ASP.NET Core 2.2,非常顺利。
但是,我们在 Windows 上托管在 Azure App Service 上,这又似乎是 运行 IIS。
现在我们在 web.config
中有一个自定义部分来限制最大内容长度,因此当用户上传文件时,他们在实际上传到限制之前知道如果上传失败:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="api/0.1/files">
<system.web>
<httpRuntime maxRequestLength="409600" executionTimeout="3600"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="419430400" />
</requestFiltering>
</security>
</system.webServer>
</location>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore"
path="*"
verb="*"
modules="AspNetCoreModule"
resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="dotnet"
arguments=".\webapp.dll"
stdoutLogEnabled="false"
stdoutLogFile="\?\%home%\LogFiles\stdout"
hostingModel="OutOfProcess"
forwardWindowsAuthToken="false"/>
</system.webServer>
</location>
</configuration>
现在,调用路由 api/0.1/files
(当然还有所有路由 "beneath" 文件)将产生 404 not found 结果并显示以下错误消息:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
我能找到的唯一解决方法是全局限制内容长度:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore"
path="*"
verb="*"
modules="AspNetCoreModule"
resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="dotnet"
arguments=".\webapp.dll"
stdoutLogEnabled="false"
stdoutLogFile="\?\%home%\LogFiles\stdout"
hostingModel="OutOfProcess"
forwardWindowsAuthToken="false"/>
<security>
<requestFiltering>
<!--400 MByte-->
<requestLimits maxAllowedContentLength="419430400" />
</requestFiltering>
</security>
</system.webServer>
</location>
</configuration>
为特定路线设置限制的正确方法是什么?
可以使用location为特定路线设置限制。
此外,如果您想更改特定 MVC 操作或控制器的最大请求正文大小限制,您可以使用 RequestSizeLimit
属性。
// GET api/values/5
[HttpGet("{id}")]
[RequestSizeLimit(40000000)]
public ActionResult<string> Get(int id)
{
return "value";
}
它设置此操作方法允许的最大请求长度。您可以在操作级别或控制器级别应用此属性。这是在 ASP.NET 核心 MVC 应用程序中增加限制的推荐方法。