【问题标题】:How to exclude auto generated web.config file when doing web deploy ASP.NET Core API (.NET 5)进行 Web 部署 ASP.NET Core API (.NET 5) 时如何排除自动生成的 web.config 文件
【发布时间】:2021-10-08 00:31:49
【问题描述】:

我有 ASP.NET Core (.NET 5) API 并希望使用 web deploy Visual Studio 进行部署。
同样在 Windows 服务器上,我在 web.config 文件中配置了 HTTPS。 因此,在发布新版本的 API 后,web.config 文件更新,我的配置丢失了。

您能否推荐一个避免这种情况的解决方案?

【问题讨论】:

  • 将 web.config 文件放入您的项目中,以便对其进行版本控制并与您的应用一起发布?
  • @MartinCostello 感谢您的评论,是的,它解决了我的问题,但我想获得一个没有 物理 web.config 文件的解决方案。

标签: c# visual-studio asp.net-core https webdeploy


【解决方案1】:

您可以按照here 的描述使用 web.config 自定义转换

自定义转换在构建配置、配置文件和环境转换之后最后运行。

为需要 web.config 转换的每个自定义配置添加一个 {CUSTOM_NAME}.transform 文件。

在以下示例中,在 custom.transform 中设置了自定义转换环境变量:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location>
    <system.webServer>
      <aspNetCore>
        <environmentVariables xdt:Transform="InsertIfMissing">
          <environmentVariable name="Custom_Specific" 
                               value="Custom_Specific_Value" 
                               xdt:Locator="Match(name)" 
                               xdt:Transform="InsertIfMissing" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

在将 CustomTransformFileName 属性传递给 dotnet publish 命令时应用转换:

dotnet publish --configuration Release /p:CustomTransformFileName=custom.transform

配置文件名称的 MSBuild 属性是 $(CustomTransformFileName)。

之前已经发布过类似的问题: Can I override a connection string in the web.config for local development?

我认为建议的最佳解决方案如下:

Web.config

<configuration>
  <connectionStrings configSource="connectionstrings.config">
  </connectionStrings>
</configuration>

connectionstrings.config(不在源代码管理中)

<connectionStrings>
  <add name="cs" connectionString="server=.;database=whatever;"/>
</connectionStrings>

每个开发者都可以选择自己本地机器指向哪个数据库,只要connectionstrings.config文件不在源代码管理中(加入忽略列表),就不会有人踩对方的脚。

【讨论】:

    【解决方案2】:

    为开发和发布环境使用不同的 Web 配置。 或者您可以将其从建筑物中排除。为此,在 web.config 属性中将“构建操作”更改为“无”,将“复制到输出目录”更改为“不复制”

    【讨论】:

    • 感谢您的回答,但我想获得一个没有物理 web.config 文件的解决方案。
    • @VanikHakobyan 如果您使用 CI/CD 工具,您可以在构建和发布工件中排除配置文件。
    • 不,我正在使用网络部署
    • @VanikHakobyan 我更新了我的答案。在您使用网络部署的情况下,您可以使用第二种方法。
    • 网络配置文件自动生成。所以我的项目中没有那个文件。
    猜你喜欢
    • 2020-10-31
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多