I’m trying to do a bunch of environment-specific configuration in an asp.net MVC3 web application I’m building. I’m somewhat new to the platform and am running into a bit of an issue with environment specific configuration.
I have 3 configuration files:
- Web.config
- Web.Debug.config
- Web.Release.config
I’ve placed my connectionString configuration in the Debug/Release files and it works, so I know asp.net recognizes at least some notion of my Debug/Release setup.
I’ve also added some custom configuration sections in Web.config like this:
<configSections>
<section name="GitHubConfig" type="SmartGigs.Controllers.GitHubConfig" />
<section name="JanRainConfig" type="SmartGigs.Services.JanRainConfig" />
</configSections>
Then I actually define them like this in Web.config:
<GitHubConfig ClientId="c" Secret="s" />
And attempt to load them like this:
var jrConfig = (JanRainConfig)WebConfigurationManager.GetSection("JanRainConfig");
var gitHubConfig = (GitHubConfig) WebConfigurationManager.GetSection("GitHubConfig");
Finally, I add a Debug-specific property into Web.Debug.config:
<JanRainConfig Key="key" TokenUrl="http://localhost:55739/Account/LogOn" />
The problem
If I get the property I defined in Web.config, it works perfectly. If I try to only define a property in Web.config or define it in Web.Debug.config and override it (and add xdt:Transform=”Replace”), the property settings are either blank (in the former case) or contain the settings in Web.config (in the latter) – as though my configuration in Web.Debug.config is being ignored.
What is the correct way to accomplish what I’m trying to do?
Web.config transformations are only applied when publishing a project. You are certainly not the first, to get this wrong (me included). I suggest using a debug-configuration in the default web.config and apply only custom logic in the .release.config file.
It is somewhat sad, that the transformed web.config isn’t picked up by VS when starting the project directly, but AFAIR the transformation process is a custom MSBuild Task, so one might be able to add execute it during compilation.