5

I have a webb aplication where I want to take advantage of the web.debug.config file so that when debugging I use a test database. This does not work for me. I have this in my web.debig.config..

<connectionStrings>
  <add name="connStr" 
    connectionString="Data Source=LocalSqlserverName;Initial Catalog=testdb;Persist Security Info=True;User ID=Myusername;Password=mypassword" 
    xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

And in my web.config file this connectionstring point to another database server. But when I debug I can see that the connectionstring in web.config file is used instead of the one in web.debug.config. What am I doing wrong here?

MTplus
  • 2,077
  • 4
  • 34
  • 51
  • if you deploy your application it would work, but if you debug your application with VS it wouldn't work and always using the `web.config` file – esiprogrammer Sep 14 '17 at 08:41
  • For real? what a worthless option to have a solution from MS... :( – MTplus Sep 14 '17 at 08:42
  • @esiprogrammer Are you sure about this? I could have sworn to used this in the past with success. Anyway, most of the time I'm using an `xdt:Transform="Replace"` instead of the `SetAttributes` option, but it shouldn't matter. – Jan_V Sep 14 '17 at 08:46
  • Previously I tried and I had no luck, here is a thread about this : [https://forums.asp.net/t/1532038.aspx](https://forums.asp.net/t/1532038.aspx) – esiprogrammer Sep 14 '17 at 08:49
  • @MTplus If an answer is correct, please mark it accordingly. The same for other questions you have asked. – Ogglas Sep 14 '17 at 09:16

1 Answers1

9

As @esiprogrammer says it normally only transforms on Publish from Visual Studio.

However you can transform Web.config on build. Add this to your *.csproj file:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild">
    <TransformXml 
        Source="Web.Base.config" 
        Transform="Web.$(Configuration).config" 
        Destination="Web.config" />
</Target>

Keep the origin configuration in Web.Base.config. It's enough to enable transformation and it works for any XML config file.

Source:

https://stackoverflow.com/a/35561167/3850405

Ogglas
  • 62,132
  • 37
  • 328
  • 418