直到现在,我们一直在ASP.NET Core 1.1上运行我们的服务。我们刚刚升级到ASP.NET Core 2.2,运行非常顺利。
但是,我们托管在Windows上的Azure应用服务上,该服务似乎又在运行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
(当然,所有路由都在文件“下面”)将产生404找不到结果,并显示以下错误消息:
您要查找的资源已被删除,名称已更改或暂时不可用。
我能找到的唯一解决方法是全局限制内容长度:
<?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 Core MVC应用程序中的限制。