Search code examples
c#xmlweb-config-transform

Web.config transform not transforming VS 2017


I have literally uncommented the boilerplate code and tried to publish to the file system to check if the "transform" works.

However, it does not transform the web.config file. I have look at the some articles and answers on here but cannot get it to work.

Things I tried:

Removing the namespace from the configuration node(complains about xdt missing)
Creating a new transform file(production)
Creating a new configuration(production)

Here is the web.Release.config:

<?xml version="1.0"?>

<!-- For more information on using Web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=301874 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator
    finds an attribute "name" that has a value of "MyDB".

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

I get the following error:

No element in the source document matches '/configuration/connectionStrings/add[@name='MyDB']'

But as you can see it is present in the add node.

Any suggestions what to do? I am confused why it isn't working.

Edit: web.config connection string

<connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=.\SQLExpress;database=smartDB;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>

Solution

  • The value of the name attribute needs to match between your web.config and web.release.config, since you are specifying Match(name) in the transformation config.

    In your web.config you have: name="DefaultConnection" In your web.release.config: name="MyDB"