Publish is not transforming web.config?
Asked Answered
C

8

17

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?

Circumvent answered 25/3, 2011 at 14:51 Comment(5)
when you publish is it building for staging or release?Mackler
@Mackler as the Build log says, it is compiling in Staging. There is a transform file for Release, but it also doesn't work.Circumvent
Do you see a message indicating that it's performing the transformation? Like mine says: "Transformed Web.config using Web.Beta.config into obj\Beta\TransformWebConfig\transformed\Web.config."Steading
@Coding yes, but there are some errors before, I've just noticed it... Updated my question...Circumvent
Any chance you could post the first part of the offending web.config file?Alecalecia
C
17

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>
Circumvent answered 1/4, 2011 at 13:22 Comment(1)
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.Greenstone
K
26

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.

Kurdistan answered 8/10, 2014 at 19:57 Comment(4)
You did indeed save me a headache.Rutan
This is the real answer.Quintie
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!Sosa
My problem ended up being not the configuration but the platform. I was publishing Any CPU but only had solution configuration set for Mixed Platforms.Candra
C
17

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>
Circumvent answered 1/4, 2011 at 13:22 Comment(1)
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.Greenstone
P
6

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.

Purkey answered 17/2, 2016 at 20:10 Comment(0)
E
3

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>
Embouchure answered 28/2, 2020 at 19:10 Comment(0)
J
0

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...

Jungian answered 23/5, 2013 at 9:2 Comment(1)
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"/>Archeology
H
0

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.

Honorarium answered 12/3, 2015 at 17:6 Comment(0)
C
0

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

Camisole answered 24/7, 2019 at 8:48 Comment(0)
S
0

@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.

Shanty answered 19/5, 2022 at 16:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.