【发布时间】:2013-10-15 05:45:33
【问题描述】:
我有一个 ASP.NET Web API 项目,其中包括 2 个配置转换:
- Web.Live.config
- Web.UAT.config
如果我在发布时选择Live 或UAT 配置,则转换不会应用于呈现的web.config 文件。
我检查了我的转换配置,name、xdt:Transform 和 xdt:Locator 是正确的。
在我的web.config 我有:
<connectionStrings>
<add name="foo" providerName="System.Data.SqlClient" connectionString="[main connection string]" />
</connectionStrings>
在我的web.Live.config 我有:
<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="foo"
connectionString="[live connection string]"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
</configuration>
但是,即使选择了Live,我发布的连接字符串仍然显示如下:
<connectionStrings>
<add name="foo" providerName="System.Data.SqlClient" connectionString="[main connection string]" />
</connectionStrings>
发生这种情况的可能原因有哪些?
【问题讨论】:
-
你发表的怎么样了?您使用的是“发布配置文件”吗?根据您发布的“连接”类型(例如 Web 部署),您将获得特定的“设置”,允许您设置连接字符串。我不确定,但我想知道这是否设置为您的主连接字符串并且优先于您的配置转换?至少可能值得一看(取消选中“在运行时使用此连接字符串”)。除此之外,配置文件本身看起来还可以
-
@musefan 谢谢,我尝试在
appSettings上执行xdt:Transform="Replace",这些更改已应用于渲染配置!因此,这绝对是连接字符串部分的问题。但是,当我在 VS2012 中“预览变换”时,它确实应用了变换 -
呵呵,不知道你可以预览变换,太酷了。无论如何,你是如何进行发布的?如果预览有效,那么它一定是在转换后覆盖它的其他东西
-
啊,我不认为“文件系统”有这个选项。只有“Web Deploy”和“Web Deploy Package”似乎……回到了思考板!
-
@musefan 解决了!我对这两种配置类型都使用了 1 配置文件。我已对此进行了更改,因此每种配置类型都有自己的配置文件,现在可以正常工作。请随时发布答案,以便我标记它!干杯。 P.S:缪斯太棒了!
标签: asp.net configuration asp.net-web-api publish web-config-transform