【发布时间】:2020-02-13 11:40:20
【问题描述】:
直到现在,我们一直在 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(当然还有所有“位于”文件下的路由)将产生 404 not found 结果,并显示以下错误消息:
您要查找的资源已被删除、更改名称或暂时不可用。
我能找到的唯一解决方法是全局限制内容长度:
<?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>
为特定路线设置限制的正确方法是什么?
【问题讨论】:
-
您是否尝试在您的操作中使用
[RequestSizeLimit(...)]?更多详情请参考document
标签: c# asp.net-core iis azure-web-app-service asp.net-core-2.2