用于 Web 应用程序项目部署的 Web.config 转换语法 【转载】

该文转自MSDN :

http://msdn.microsoft.com/zh-cn/library/dd465326(v=VS.100).aspx

Web.config 文件通常包括根据应用程序的运行环境而必须不同的设置。例如,在部署 Web.config 文件时,您可能必须更改数据库连接字符串或禁用调试。对于 Web 应用程序项目,ASP.NET 提供了一些工具,用于自动完成在部署这些项目时更改(转换)Web.config 文件的过程。对于要部署到的每个环境,您将创建一个转换文件,该文件仅指定原始 Web.config 文件和适用于该环境的已部署 Web.config 文件之间的差异。

转换文件是一个 XML 文件,该文件指定在部署 Web.config 文件时应如何更改该文件。转换操作通过使用在 XML-Document-Transform 命名空间(映射到 xdt 前缀)中定义的 XML 特性来指定。XML-Document-Transform 命名空间定义两个特性:Locator 和 Transform。Locator 特性指定要以某种方式更改的 Web.config 元素或一组元素。Transform 特性指定要对 Locator 特性所查找的元素执行哪些操作。

下面的示例演示了转换文件的内容,该转换文件将更改连接字符串并替换 customErrors 元素:

复制

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="MyDB" 
      connectionString="value for the deployed Web.config file" 
      xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>
  <system.web>
    <customErrors defaultRedirect="GenericError.htm"
      mode="RemoteOnly" xdt:Transform="Replace">
      <error statusCode="500" redirect="InternalError.htm"/>
    </customErrors>
  </system.web>
</configuration>

转换文件的根元素必须在其开始标记中指定 XML-Document-Transform 命名空间,如前面的示例所示。Locator 和 Transform 元素本身不会在部署的 Web.config 文件中重现。

原文地址:https://www.cnblogs.com/chenxizhang/p/1743236.html