Msbuild 如何在转换时将configSource转换为web.config中的内联元素

Msbuild 如何在转换时将configSource转换为web.config中的内联元素,msbuild,web-config,msdeploy,web-config-transform,config-transformation,Msbuild,Web Config,Msdeploy,Web Config Transform,Config Transformation,当前my web.config的外观如下所示: <configuration> <connectionStrings configSource="connectionstrings.config"/> </configuration> 生成部署包(Msbuild.exe+target=package)时,它不会“神奇地”参数化到parameters.xml文件的连接字符串,以便在部署时进行替换 当我内联我的连接字符串时,一切都很好,并且为我的连接字符

当前my web.config的外观如下所示:

<configuration>
  <connectionStrings configSource="connectionstrings.config"/>
</configuration>

生成部署包(Msbuild.exe+target=package)时,它不会“神奇地”参数化到parameters.xml文件的连接字符串,以便在部署时进行替换

当我内联我的连接字符串时,一切都很好,并且为我的连接字符串生成了参数

因此:

如何通过web.config转换复制connectionstrings.config的内容以替换部署中的内容

编辑:
我接受了Sayed Ibrahim的答案,因为“默认”行为非常好(web.config中连接字符串的自动参数化),但最终最好明确指定哪些内容需要参数化(通过{projectname}.wpp.targets或parameters.xml文件)。

你问错了问题,不用担心如何转换回web.config。相反,只需在文件中为连接字符串创建参数即可。我刚刚在博客上写了这个。我还为您粘贴了下面的内容

如果您在VS 2010或VS 11中使用了Visual Studio web publish来创建web部署包,那么您可能知道我们会自动在web.config中参数化连接字符串。如果您不熟悉Web部署参数,则可以通过这些参数声明您希望在以后发布包时能够轻松地更新某些内容的值。连接字符串是在发布过程中通常需要更新的内容的好例子

如前所述,如果您在Visual Studio中创建Web部署包,我们将在Web.config中为所有连接字符串自动创建Web部署参数。今天早些时候,我看到一个问题(实际上问了其他问题,但我认为这是他真正想要的)。我创建了一个示例,演示如何执行此操作。下面是web.config中ConnectionString元素的外观

<connectionStrings configSource="connectionStrings.config" />

这里是connectionStrings.config

<connectionStrings>
  <clear/>
  <add name="ApplicationServices"
       connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
       providerName="System.Data.SqlClient" />
  <add name="OtherConnectionString"
       connectionString="data source=.\SQLExpress;Integrated Security=SSPI;Initial Catalog=foo"
       providerName="System.Data.SqlClient"/>
</connectionStrings>


  • Scope=需要修改的文件的正则表达式
  • Match=要更新的属性/元素的XPath表达式
  • Description=可选描述(如果导入了pkg,则该描述将显示在IIS管理器中)
  • DefaultValue=可选参数标记的默认值=可选,对于连接字符串,请使用SqlConnectionString
  • 创建此文件后,需要关闭/重新打开VS(它缓存导入的.targets文件)。然后您可以创建一个web部署包。执行此操作时,将声明这些新参数。在我的例子中,我在IIS管理器中导入了它,下面是显示参数的对话框。

    正如您所看到的,应用程序路径参数以及我的自定义连接字符串值都显示在这里。当我更新文本框中的值并在web服务器上打开ConnectionString.config时,它们就是我在对话框中输入的值


    仅供参考,我已将此示例上载到我的github帐户。

    不需要添加包含详细MsBuild语法的{projectname}.wpp.targets。您还可以在webroot中添加一个parameters.xml文件,该文件的语法与parameters.xml文件(在部署包中找到)相同。查看我的github帐户以获取示例:。我应该将此列为一个选项,我将在今晚晚些时候更新此帖子,并提供有关此方法的详细信息。它们都归结为同一件事(parameters.xml被转换为我展示的MSBuild项)。
    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
      <ItemGroup>
        <!-- Here we need to declare MSDeploy parameters for connection strings in connectionStrings.config -->
        <MsDeployDeclareParameters Include="ApplicationServices-ConnectionString" >
          <Kind>XmlFile</Kind>
          <Scope>connectionStrings.config$</Scope>
          <Match>/connectionStrings/add[@name='ApplicationServices']/@connectionString</Match>
          <Description>Connection string for ApplicationServices</Description>
          <DefaultValue>data source=(localhost);Initial Catalog=AppServices</DefaultValue>
          <Tags>SqlConnectionString</Tags>
        </MsDeployDeclareParameters>
    
        <MsDeployDeclareParameters Include="OtherConnectionString-ConnectionString" >
          <Kind>XmlFile</Kind>
          <Scope>connectionStrings.config$</Scope>
          <Match>/connectionStrings/add[@name='OtherConnectionString']/@connectionString</Match>
          <Description>Connection string for OtherConnectionString</Description>
          <DefaultValue>data source=(localhost);Initial Catalog=OtherDb</DefaultValue>
          <Tags>SqlConnectionString</Tags>
        </MsDeployDeclareParameters>
      </ItemGroup>
    
    </Project>