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

如何在.NET Core 2发布到IIS时自定义web.config文件?

当然可以!再也不用每次发布后手动折腾web.config了,Visual Studio给咱们提供了好几种靠谱的方式来自定义发布时生成的这个配置文件,我给你梳理几个最实用的:

方法一:用项目根目录的web.config作为模板

这是最简单直接的方式,适合大多数场景:

  • 在你的.NET Core 2 Web应用项目根目录,手动添加一个web.config文件(如果还没有的话)
  • 把你需要的自定义配置(比如文件上传限制、HTTPS重定向)直接写进去,示例如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <!-- 自定义的文件上传大小限制(100MB) -->
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="104857600" />
      </requestFiltering>
    </security>
    <!-- HTTPS重定向规则 -->
    <rewrite>
      <rules>
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
    <!-- 注意:VS发布时会自动添加aspNetCore模块的必要配置,不用手动写 -->
  </system.webServer>
</configuration>
  • 发布的时候,VS会自动把.NET Core运行时需要的配置(比如<aspNetCore>节点)合并到这个模板文件里,而不是完全覆盖它。这样你的自定义配置就会被保留下来,不用每次手动修改。

方法二:用发布配置文件做XML变换(适合多环境配置)

如果你需要针对不同的发布环境(比如开发、生产)使用不同的web.config配置,这个方法就很合适:

  • 打开项目的Properties/PublishProfiles目录,找到你的发布配置文件(比如FolderProfile.pubxml)
  • 右键这个文件,选择「添加配置变换」,VS会生成一个对应的变换文件(比如FolderProfile.pubxml.web.config)
  • 在这个变换文件里,用XML变换语法来添加或修改配置,示例如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.webServer>
    <!-- 设置文件上传大小为100MB -->
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="104857600" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
      </requestFiltering>
    </security>
    <!-- 插入HTTPS重定向规则(如果原本没有的话) -->
    <rewrite xdt:Transform="InsertIfMissing">
      <rules xdt:Transform="InsertIfMissing">
        <rule name="HTTP to HTTPS redirect" stopProcessing="true" xdt:Transform="Insert">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
  • 每次发布时,VS会自动把这些变换规则应用到生成的web.config里,自动帮你完成配置修改。

方法三:完全禁用自动生成web.config(适合完全自定义)

如果你想完全掌控web.config的内容,不让VS自动修改它,可以这么做:

  • 打开你的项目文件(.csproj),添加以下MSBuild属性:
<PropertyGroup>
  <!-- 禁用VS自动生成web.config -->
  <GenerateWebConfig>false</GenerateWebConfig>
</PropertyGroup>
  • 这时候你需要在项目根目录的web.config里手动添加.NET Core运行时必需的<aspNetCore>节点,示例如下:
<aspNetCore processPath="dotnet" arguments=".\YourApp.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
  • 这样发布时,VS会直接用你写的web.config,不会做任何修改。

总结

  • 日常开发推荐用方法一,简单省心;
  • 多环境不同配置需求用方法二;
  • 完全自定义配置用方法三。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 04:05:18