【问题标题】:How to set up Web.Config transformation to cater for Local and production configurations?如何设置 Web.Config 转换以适应本地和生产配置?
【发布时间】:2015-09-22 22:50:22
【问题描述】:

我有这样的两难境地:我的本地计算机上的 web.config 必须有不同的连接字符串,并且必须进行发布转换,以使生产二进制文件使用 web 服务器上的 machine.config。

所以我的 Visual Studio 解决方案中有这些文件:

Web.Config Web.Debug.Config Web.Release.Config

在 web.config 中,我删除并添加了新的连接字符串。

<remove name="connstring">
<add name="connstring" ConnectionString="blahblah" />

我想要做的是在部署(通过 TFS 构建)到 web 服务器时在最终 web.config 中没有任何内容,以便我的 web 应用程序可以使用服务器上 machine.config 中的任何内容。

我该怎么做?

【问题讨论】:

    标签: c# asp.net web-config


    【解决方案1】:

    您可以使用 RemoveAll 转换属性删除配置设置。假设您正在部署 Release 构建配置,您可以通过将以下内容放入 Web.Release.Config 来生成一个完全空的 web.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform" xdt:Transform="RemoveAll" />
    

    生成的 web.config 将在顶部包含 XML 声明,没有其他内容。

    如果您只想删除某些配置子部分,请将 RemoveAll 转换属性添加到要删除的部分。例如,以下 Web.Release.Config 将删除所有应用程序设置:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <appSettings xdt:Transform="RemoveAll" />
    </configuration>
    

    查看完整的Transformation Syntax Documentation 了解更多详情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-29
      • 2012-07-05
      • 2011-07-30
      • 2017-08-18
      • 2017-11-26
      • 2013-10-14
      • 2013-10-18
      相关资源
      最近更新 更多