Reputation: 3665
We have a production server and a development server for our site. I have different settings and license information for each and that is held in 2 different web.config files.
Is there a way to consolidate those to one web.config file? So that I don't overwrite production's with development's (or vice versa) when I publish the site.
Upvotes: 0
Views: 290
Reputation: 45083
Just one possibility, but you could merge them giving appropriate names to values based on the context and access each element conditionally using compilation constants in code. For instance, in your web.config you may have...
<connectionStrings>
<add name="DevelopmentConnection" connectionString="DevelopmentConnectionString" />
<add name="DeploymentConnection" connectionString="DeploymentConnectionString" />
</connectionStrings>
Then, within, let's say, a Configuration class you may have...
public static class Configuration
{
public string ConnectionString
{
get
{
#if DEBUG
return ConfigurationManager.ConnectionStrings["DevelopmentConnection"].ConnectionString;
#else
return ConfigurationManager.ConnectionStrings["DeploymentConnection"].ConnectionString;
#endif
}
}
}
Now, considering the DEBUG constant is defined, then, when building per environment, using this property would pull through the appropriate setting.
Upvotes: 0
Reputation: 2481
With VS 2010, you can do this with web.config transformations and build configurations. Scott Guthrie wrote about this:
http://weblogs.asp.net/scottgu/archive/2010/07/29/vs-2010-web-deployment.aspx
I think this basically works by you just creating your additional config files (such as Web.Debug.config) and VS automatically "links" them to your main web.config in the IDE, but I can't verify it at the computer I am using right now since it doesn't have VS 2010.
You can also do it fairly easily with build events in older versions of VS, but it isn't as nice. Scott actually wrote about this too:
(Taken from Scott's blog post) Here the high-level steps you take to do this. They work with both VS 2005 and VS 2008.
Upvotes: 1
Reputation: 23226
You can create a web deployment project (VS2008, VS2010) which is an XML MSBuild file that lets you replace/insert/substitute XML nodes into the configuration file when you build for a particular profile. You'll probably spend a half a day to a day getting it configured correctly but the time is well worth it as it can save you a lot of deployment headaches (also puts you one step closer to one-click deployments).
Note: for the VS2008 web deployment projects you will probably want to use the MSBuild Community Tasks. I don't know what the status is for VS2010 with these tasks - whether or not they are still required.
For our VS2008 web deployment tasks using the MSBuild Community Tasks (which we use under VS2010) it looks a little like...
<!-- ... -->
<Target Name="AfterBuild">
<XmlMassUpdate Condition="'$(Configuration)|$(Platform)' == 'Release - Dev|AnyCPU'" ContentFile="$(OutputPath)\web.config" SubstitutionsFile="$(OutputPath)\web.Substitutions.config" ContentRoot="/" SubstitutionsRoot="/configuration/substitutions/dev" />
</Target>
<!-- ... -->
In our web application's directory we have a file web.Substitutions.config
which looks a bit like...
<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:xmu="urn:msbuildcommunitytasks-xmlmassupdate">
<substitutions>
<!-- dev settings-->
<dev>
<configuration>
<appSettings file="">
<add xmu:key="key" key="some_setting" value="a_value_is_here"/>
</appSettings>
</configuration>
</dev>
<prod> <!-- you get the idea --></prod>
</substitutions>
</configuration>
Worth mentioning that we use Team City to run our web deployment projects with no additional configuration necessary and it works without a hitch. Hudson is the same way (and a bit easier to setup).
Upvotes: 0