You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 5 Azure App Service大文件上传配置如何通过Azure Pipelines自动写入web.config

完全可以实现,以下是两种最常用的落地方式,可根据你的团队习惯选择:

方案1:项目内置web.config模板(无需修改管道)

.NET Core/.NET 5+虽然默认不生成web.config,但你可以主动在项目根目录新建web.config文件,填入你需要的配置内容:

<configuration>
    <system.webServer>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="500000000" />
          </requestFiltering>
        </security>
    </system.webServer>
</configuration>  

项目发布时,SDK会自动合并你自定义的web.config和运行时需要的默认配置,不会覆盖你设置的上传大小限制,直接把这个文件提交到代码仓库,每次部署都会自动带上配置,不需要改动现有Azure Pipelines逻辑。

方案2:Azure Pipelines动态注入配置(无需提交web.config到代码库)

如果不想把web.config放到代码仓库,可以在管道的发布构建产物之后、部署到Azure App Service之前,加一个PowerShell任务,自动生成或修改web.config:

  1. 新建PowerShell任务,粘贴以下脚本:
# 替换为你实际的发布产物路径
$webConfigPath = "$(Build.ArtifactStagingDirectory)/web.config"

# 如果已经存在web.config则修改,不存在就新建
if (Test-Path $webConfigPath) {
    $xml = [xml](Get-Content $webConfigPath -Encoding UTF8)
    
    # 逐级检查节点是否存在,不存在则创建
    $systemWebServer = $xml.configuration["system.webServer"] ?? $xml.configuration.AppendChild($xml.CreateElement("system.webServer"))
    $security = $systemWebServer["security"] ?? $systemWebServer.AppendChild($xml.CreateElement("security"))
    $requestFiltering = $security["requestFiltering"] ?? $security.AppendChild($xml.CreateElement("requestFiltering"))
    $requestLimits = $requestFiltering["requestLimits"] ?? $requestFiltering.AppendChild($xml.CreateElement("requestLimits"))
    
    # 设置上传大小限制
    $requestLimits.SetAttribute("maxAllowedContentLength", "500000000")
    $xml.Save($webConfigPath)
}
else {
    $configContent = @"
<configuration>
    <system.webServer>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="500000000" />
          </requestFiltering>
        </security>
    </system.webServer>
</configuration>  
"@
    Set-Content -Path $webConfigPath -Value $configContent -Encoding UTF8
}
  1. 正常执行后续的Azure App Service部署步骤即可,修改后的配置会同步部署到服务端。

内容的提问来源于stack exchange,提问作者Walnut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 13:57:03