Publish is not transforming web.config?

18,103

Solution 1

I found out two things:

  • You cannot set a namespace on the <configuration> tag (ex: for <location path="." inheritInChildApplications="false">)
  • You have to watch for the correct hierarchy in the transform file.

Like

<configuration>
  <location>
    <connectionStrings>

Instead of

<configuration>
  <connectionStrings>

Solution 2

Answering late but perhaps I can save someone a headache. In Visual Studio 2013, there are two places to select configuration for your build and deploy. The Configuration Manager and then again with Publish Web where the third step in the Wizard entitled Settings allows you to select Config you want to use. If you don't select your new configuration it will use the transform for the selected configuration instead of yours.

Solution 3

Ensure that in the properties of the Web.Config file Build Action is set to Content.

If the build action is set to None, it will not be transformed, even if it is being copied to the output directory.

Solution 4

Make sure to include InsertIfMissing if the section you are trying to add does not already appear in the output.

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location>
    <system.webServer>

      <security xdt:Transform="InsertIfMissing">
        <requestFiltering allowDoubleEscaping="true" />
      </security>

    </system.webServer>
  </location>
</configuration>
Share:
18,103
BrunoLM
Author by

BrunoLM

I'm a Developer for Fun! Things I like Code Play games Anime / Manga Contact information [email protected] LinkedIn Facebook Site - https://brunolm.github.io/ Blog - http://blog.codingwise.com/

Updated on June 13, 2022

Comments

  • BrunoLM
    BrunoLM almost 2 years

    I made a web.config (full file, it doesn't show XML errors)

    <?xml version="1.0"?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
      <configSections>
          ...
          <location path="." inheritInChildApplications="false">
            <connectionStrings>
              <add name="ElmahLog" connectionString="data source=~/App_Data/Error.db" />
              <add name="database" connectionString="w" providerName="System.Data.EntityClient"/>
            </connectionStrings>
          </location>
      ...
    

    with a transform file (web.Staging.config)

    <?xml version="1.0"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
      <connectionStrings>
        <add name="database"
          connectionString="c"
          providerName="System.Data.EntityClient"
          xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
      </connectionStrings>
      <system.web>
        <compilation xdt:Transform="RemoveAttributes(debug)" />
        <customErrors defaultRedirect="error.aspx"
          mode="RemoteOnly" xdt:Transform="Replace">
        </customErrors>
      </system.web>
    </configuration>
    

    I am publishing in Staging mode (right click website > Publish > Method: File System ...)

    ------ Build started: Project: Drawing, Configuration: Staging Any CPU ------
      Drawing -> D:\Project\bin\Staging\Drawing.dll
    ------ Build started: Project: MySystem, Configuration: Staging Any CPU ------
      MySystem -> D:\Project\bin\Staging\MySystem.dll
    ...
    

    But when I look at the web.config in the output folder it isn't changed.

    I found the following on the Build log:

    D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
    D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
    D:\Project\Web.Staging.config(3,2): Warning : No element in the source document matches '/configuration'
    Transformed web.config using Web.Staging.config into obj\Staging\TransformWebConfig\transformed\web.config.
    

    What could be the problem? Am I doing this right?

  • amartynov
    amartynov about 11 years
    Regarding xmlns: actually you can use it, but then you have to add it to the transform root tag too. Without xmlns it works, but VS does not recognize the inheritInChildApplications attribute.
  • Josh M.
    Josh M. almost 10 years
    That's not true. You can specify just the attributes that are changing and instruct the transform to only update the attributes (not replace the element): <compilation debug="false" xdt:Transform="SetAttributes"/>
  • izb
    izb over 9 years
    You did indeed save me a headache.
  • Roy Oliver
    Roy Oliver almost 8 years
    This is the real answer.
  • puddleglum
    puddleglum over 5 years
    In the publish settings, mine had the correct configuration (release) but transform was not working. I changed it to debug, then saved, then changed it back to release, and it started working fine. thanks for the pointer!