web.config transform - delete comments from connectionstring section

▼魔方 西西 提交于 2019-12-04 15:36:05

问题


I store several different connection strings in my web.config for development and testing. All but one is commented out so I can change info as needed.

When I publish, I would like to replace everything (including comments) in the connectionStrings node with this:

<add name="myDb" connectionString="Data Source={SERVER};Initial Catalog=ManEx;User Id={USER};Password={PASSWORD};" providerName="System.Data.SqlClient"  />
<!--<add name="myDb" connectionString="Data Source={SERVER};Initial Catalog=ManEx;Integrated Security=True" providerName="System.Data.SqlClient" />-->

I know how to change the active string with this:

<add name="myDb"
     connectionString="Data Source={SERVER};Initial Catalog=ManEx;User Id={USER};Password={PASSWORD};"
     providerName="System.Data.SqlClient"
     xdt:Transform="Add" 
     xdt:Locator="Match(name)"/>

But I don't know how to clear out the comments I don't want and add the comment I do want.

Any ideas?


回答1:


Instead of transforming the string, or using "Remove" and "Insert" clean the section try using "Replace".

For example:

<connectionStrings xdt:Transform="Replace">

    <add name="myDb"
         connectionString="Data Source={SERVER};Initial Catalog=ManEx;User Id={USER};Password={PASSWORD};"
         providerName="System.Data.SqlClient" />

</connectionStrings>

You can configure this section exactly how you want it, even if that means you add new comments.




回答2:


What I did based on this answer was the following:

  • Removed the existing connectStrings section in Web.config which contains commented out connection strings used during debug time;
  • Re-added the connectionStrings section with the correct connection string to be used when the app is deployed.

So in your Web.config transform file you have something like this:

<!-- Removes the existing connectionStrings section which contains internal connection strings used for debugging -->
<connectionStrings xdt:Transform="Remove">    
</connectionStrings>

<!-- Re-adding the existing connectionStrings section -->
<connectionStrings xdt:Transform="Insert">

<add name="MyConnectionStringName" connectionString="Data Source=CLUSTERSQL;Initial Catalog=MyDatabase;Integrated Security=True;multipleactiveresultsets=True" providerName="System.Data.SqlClient"  xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>

</connectionStrings>



回答3:


In Visual Studio 2013, you can have several Web.config files.

Also, when you create a new project, VS creates 2 for you : Web.Debug.config and Web.Release.config. That way, you can have a different Web.Config for your debug project and for your release project




回答4:


If you need to Add/Insert/Setattributes inside a replaced connectionstring (for example if you use deployments) you can nest the transformations to remove the comments and replace the attributes:

<connectionStrings xdt:Transform="Replace">
    <add name="connectionDatabase" 
         connectionString="#{ConnectionString}" 
         xdt:Transform="SetAttributes"
         xdt:Locator="Match(name)" />
</connectionStrings>


来源:https://stackoverflow.com/questions/25213265/web-config-transform-delete-comments-from-connectionstring-section

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!