是否可以更改依赖选定的Web发布配置文件的连接字符串(或仅仅是服务器主机)?也许使用 web.config变换 还是其他?

我的意思是为了配置文件“测试”更改连接字符串“ myConnstring”(在已发布的Web.config中) "Data Source='example.com,14333;..." 以及个人资料“生产” - "Data Source=./SQLExpress;..."

有帮助吗?

解决方案

这正是Web配置转换的目的。您在帖子中提供的链接具有专门用于连接字符串的演练。

要从“转换”开始,请右键单击项目资源管理器中的Web.config文件,然后选择“添加配置转换”。假设您在解决方案配置中具有configa和configb,则将添加两个新文件,web.configa.config和web.configb.config。

如果您打开这些新文件,除了一堆评论外,它们会很空。它们实际上包含您可以使用的连接字符串示例 - 看起来像这样:

<connectionStrings>
  <add name="MyDB" 
    connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

摘要本节,然后将“名称”属性更改为基本web.config文件中连接字符串的名称。将“ Connecttring”属性设置为您要用于Configa的实际值。因此,这样:

<connectionStrings>
  <add name="myConnectionString" 
    connectionString="Data Source=ConfigASqlServer;Initial Catalog=ConfigADatabase;Integrated Security=True" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

重复Web.configb.config文件的过程,并使用ConfigB所需的连接字符串。

现在,当您在Visual Studio中使用Publish Command时,它将自动转换base Web.config文件,并将“ ConnectionsTring”属性设置为发布时所使用的任何配置。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top