BrunoLM
BrunoLM

Reputation: 100322

Publish is not transforming web.config?

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?

Upvotes: 17

Views: 20862

Answers (8)

Ramin Sadat
Ramin Sadat

Reputation: 61

@Karthikeyan VK your post resolved my issue. Although I was selecting Production configuration in my publish profile, in configuration manager it was set to dev therefore It didn't transform my settings.

Microsoft needs to fix this bug. Once you pick a configuration in the publishing profile it should automatically update the configuration manager as well.

Upvotes: 0

Simon_Weaver
Simon_Weaver

Reputation: 145880

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>

Upvotes: 3

Karthikeyan VK
Karthikeyan VK

Reputation: 6006

I followed the below steps to fix this issue. Thanks, @michaelhawkins for pointing in the right direction. You need to make sure you change the configuration to release in two places.

enter image description here

And right click on your project and select "Properties". IF not working try selecting x86 in CPU Architecture

enter image description here

Upvotes: 0

Aaroninus
Aaroninus

Reputation: 1112

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.

Upvotes: 6

Thiago
Thiago

Reputation: 21

Answering late as well, but this may help someone.

I realized that if you have two websites in the same solution, when you try to publish one of them the transformation might not work if you have one only configuration for both projects.

One of my websites was always transforming, but the other sometimes was and sometimes wasn't.

For example, I had the configuration "Auto" in the solution, and had web.Auto.config for both websites.

I resolved that by creating a new configuration with a different name - "AutoAdmin" - creating also its web.AutoAdmin.config file for the second project, and when I published it again the transformation finally occurred.

Upvotes: 0

michaelhawkins
michaelhawkins

Reputation: 1066

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.

Upvotes: 26

Stefan
Stefan

Reputation: 75

Don't forget to copy all the other attributes of "configuration" from the original "web.config", as it seems that VS2012 doesn't do it automatically and of course there will be no match...

Upvotes: 0

BrunoLM
BrunoLM

Reputation: 100322

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>

Upvotes: 17

Related Questions