In a .NET MVC 3.0 Application I have the following configuration in appSettings
:
web.config
<appSettings>
<add key="SMTPHost" value="mail.domain.com"/>
<add key="SMTPUsername" value="user@gmail.com"/>
<add key="SMTPPort" value="25"/>
<add key="SMTPPwd" value="mypassword"/>
<add key="EmailFrom" value="notific@gmail.com"/>
</appSettings>
For debugging, I have the following configuration transform defined:
web.Debug.config
<appSettings>
<add key="SMTPPort" value="58" xdt:Transform="Replace" xdt:Locator="Match(key)" />
</appSettings>
And I run the application in debug mode, but my SMTP port is still taking the value from the web.config
, not web.Debug.config
.
Can anyone suggest what could be wrong in this configuration?
Recently I had the same problem with a older web.config filed based on .NET Framework 2.0. The solution was simply remove the web.config's namespace (xmlns attibute in configuration root node):
BEFORE:
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
AFTER:
<configuration>
Answering your question isn't simple, because it poses a problem - if you want to transform Web.config with Web.debug.config - where transformation effect should be stored? In Web.config itself? This would overwrite transformation source file! Probably that's why Visual Studio doesn't do transformations during builds.
Previous Matt answer is valid, but you might want to mix them to have generic solution that works when you actually change active solution configuration from debug to release etc. Here's a simple solution:
Web.config
file toWeb.base.config
- transformations should automaticly rename accordingly (Web.base.Debug.config
, etc)Now, when you build your solution, a Web.config file will be created with valid transformations for active configuration.
The Web.config transforms are only applied as part of a publish operation.
If you wish this to be done as part of an
app.config
build operation, then you can use the SlowCheetah - XML Transforms Visual Studio plugin:http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5
Use Octopus Deploy (Community edition is free) and let it transform the
web.config
for you. Steps:Web.Release.config
has theBuild Action
property set toContent
just like your mainweb.config
file.That's it! Octopus will do the rest without any special config. A default IIS Web Site deploy will do this out of the box:
Apparently there's a extension for Visual Studio 2015
https://visualstudiogallery.msdn.microsoft.com/05bb50e3-c971-4613-9379-acae2cfe6f9e
This package enables you to transform your app.config or any other XML file based on the build configuration
Your immediate question has been answered - the explanation is that the transform is applied on publish, not on build.
However, I think that it does not offer a solution on how to achieve what you want to do.
I have been struggling with this exact problem for a few days now, looking for a way to keep web.config clean and set all keys that vary depending on environment in the respective transform files. My conclusion is that the easiest and most stable solution is to use debug values in the original web.config, that way they are always present when you do debug runs in Visual Studio.
Then create transforms for the different environments you want to publish to - test, integration, production - whatever you have. The now built-in functionality to transform web.config files on publish will suffice for this. No need for SlowCheetah or editing build events nor project files. If you have only web projects that is.
Should you wish to, you could also have the web.debug.config file in your solution, just to keep a separate file with all the values pertaining to the development environment. Be sure to comment in it that the values are not applied when running in Visual Studio though, in case someone else try to use it for that purpose!