对于要部署到的每个环境,您将创建一个转换文件,该文件仅指定原始 Web.config 文件和适用于该环境的已部署 Web.config 文件之间的差异。

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>

Transform 元素本身不会在部署的 Web.config 文件中重现。

下面各节提供有关要在转换文件中使用的语法的参考信息。

Locator 特性语法

Locator 特性的语法。

条件

选择了与组合 XPath 表达式匹配的元素。

语法

Locator="Condition(XPath expression)"

示例

在部署的 Web.config 文件中,所选元素将替换为在转换文件中指定的元素。

<configuration xmlns:xdt="...">
<connectionStrings>
<add name="AWLT" connectionString="newstring"
providerName="newprovider"
xdt:Transform="Replace"
xdt:Locator="Condition(@name='oldname'
or @providerName='oldprovider')" />
</connectionStrings>
</configuration>

Condition 表达式的结果应用于开发 Web.config 文件的有效 XPath 表达式如下所示:

configuration/connectionStrings[@name='AWLT' or @providerName='System.Data.SqlClient']

configuration/connectionStrings) 的隐式 XPath 条件与显式指定的表达式组合起来的结果。

Match

如果指定了多个特性名称,则将仅选择与所有指定特性匹配的元素。

语法

Locator="Match(comma-delimited list of one or more attribute names)"

示例

add 元素。

<configuration xmlns:xdt="...">
<connectionStrings>
<add name="AWLT" connectionString="newstring"
providerName="newprovider"
xdt:Transform="Replace"
xdt:Locator="Match(name)" />
</connectionStrings>
</configuration>

Replace

Locator 特性的示例。

语法

Transform="Replace"

XPath

Condition 不同,所指定的表达式不追加到与当前元素对应的隐式 XPath 表达式。)

语法

Locator="XPath(XPath expression)"

示例

Condition 关键字示例中所选元素相同的元素。

<configuration xmlns:xdt="...">
<connectionStrings>
<add name="AWLT" connectionString="newstring"
providerName="newprovider"
xdt:Transform="Replace"
xdt:Locator="XPath(configuration/connectionStrings[@name='AWLT'
or @providerName='System.Data.SqlClient'])" />
</connectionStrings>
</configuration>
Transform 特性语法

Transform 特性的语法。

Insert

该新元素被添加到任何集合的末尾。

语法

Transform="Insert"

示例

在部署的 Web.config 文件中,指定的连接字符串将添加到集合的末尾。

<configuration xmlns:xdt="...">
<connectionStrings>
<add name="AWLT" connectionString="newstring"
providerName="newprovider"
xdt:Transform="Insert" />
</connectionStrings>
</configuration>

InsertBefore

该 XPath 表达式必须是一个绝对表达式,因为它作为一个整体应用于开发 Web.config 文件,而不只是追加到当前元素的隐式 XPath 表达式中。

语法

Transform="InsertBefore(XPath expression)"

示例

allow 元素。

<configuration xmlns:xdt="...">
<authorization>
<allow roles="Admins"
xdt:Transform="InsertBefore(/configuration/system.web/authorization/deny[@users='*'])" />
</authorization>
</configuration>

InsertAfter

该 XPath 表达式必须是一个绝对表达式,因为它作为一个整体应用于开发 Web.config 文件,而不是追加到当前元素的隐式 XPath 表达式中。

语法

Transform="InsertAfter(XPath expression)"

示例

deny 元素。

<configuration xmlns:xdt="...">
<authorization>
<deny users="UserName"
xdt:Transform="InsertAfter
(/configuration/system.web/authorization/allow[@roles='Admins'])" />
</authorization>
</configuration>

Remove

如果选择了多个元素,则移除第一个元素。

语法

Transform="Remove"

示例

在部署的 Web.config 文件中,将仅移除第一个连接字符串元素。

<configuration xmlns:xdt="...">
<connectionStrings>
<add xdt:Transform="Remove" />
</connectionStrings>
</configuration>

RemoveAll

移除选定的一个或多个元素。

语法

Transform="RemoveAll"

示例

在部署的 Web.config 文件中,将移除所有元素。

<configuration xmlns:xdt="...">
<connectionStrings>
<add xdt:Transform="RemoveAll" />
</connectionStrings>
</configuration>

RemoveAttributes

从所选元素移除指定的特性。

语法

Transform="RemoveAttributes(comma-delimited list of one or more attribute names)"

示例

batch 特性。

<configuration xmlns:xdt="...">
<compilation
xdt:Transform="RemoveAttributes(debug,batch)">
</compilation>
</configuration>

SetAttributes

SetAttributes 特性使您能够按原样保留元素而只更改所选特性。

语法

Transform="SetAttributes(comma-delimited list of one or more attribute names)"

示例

false

<configuration xmlns:xdt="...">
<compilation
batch="false"
xdt:Transform="SetAttributes(batch)">
</compilation>
</configuration>

XSLT

将 XSLT 文件应用于所选元素。

语法

Transform="XSLT(file path)"

示例

appSettings 元素并指定在 appSettings.xslt 文件中定义的转换。

<configuration xmlns:xdt="...">
<appSettings
xdt:Transform="XSLT(C:\MyProject\appSettings.xslt)" />
</appSettings>
</configuration>
省略 Locator 特性

Locator 特性来指示其他方面。

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web xdt:Transform="Replace">
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
</system.web>
</configuration>
对独立元素使用 Transform 和 Locator 特性

Transform 特性来将更改应用于子元素。

location 元素的子元素才能进行转换。

<configuration xmlns:xdt="...">
<location path="C:\MySite\Admin" xdt:Locator="Match(path)">
<system.web>
<pages viewStateEncryptionMode="Always"
xdt:Transform="SetAttributes(viewStateEncryptionMode)" />
</system.web>
</location>
</configuration>

Transform 特性,则不会进行任何更改。

说明

system.web 元素的所有子元素将替换为转换文件中的内容。

相关文章:

  • 2021-12-13
  • 2022-03-06
  • 2022-12-23
  • 2021-07-25
  • 2022-12-23
  • 2021-12-25
  • 2022-12-23
猜你喜欢
  • 2022-01-09
  • 2021-10-26
  • 2021-06-17
  • 2021-09-17
  • 2021-08-30
  • 2022-02-01
  • 2022-12-23
相关资源
相似解决方案