views:

229

answers:

3

I have found a number of pieces of information on having multiple ASP.NET configuration files for a web deployment. However, I am still unsure if I can accomplish what I want to accomplish.

Basically, the website I am working on can be deployed to three different sites. Depending on the site that it is deployed to, the configuration settings need to be changed. What I would like to do is have configuration files for each possible configuration (full web.config files) and then be able to tell a deployment build which config file to use for a particular deployment (I can edit this manually if necessary).

Is there something as simple as pointing to a different .config file, or do I need to do something more sophisticated?

EDIT: One particular concern that I have is that I also need settings in system.net for mail settings, etc. So, I'm not looking to only override the appSettings.

Thanks

A: 

You can do this in .Net Framework 4. ScottGu shows a quick demo of it in his recent talk in Sweden. In his example he has staging, production etc. with each file having (potentially) different content.

jbloomer
Good to hear this. However, I'm still on .NET 2 and won't be upgrading in the foreseeable future.
JasCav
+1  A: 

On your main web.config add the following attribute to appSettings

<appSettings file="Web.site1.config">

Then, asp.net will see both files as one. You can edit web.config in order to include a different external file, depending on the Site.

Okay, this is what I started with. However, I also need other settings such as the SMTP settings in system.net. Does this solution that that into account? (I updated my question to reflect this.)
JasCav
look here: http://stackoverflow.com/questions/804427/storing-smtp-outside-the-web-config-file
this **only** works for <appSettings>, unfortunately. No other config sections support this.
marc_s
+2  A: 

Any configuration section - such as <smtp> - can be "externalized", e.g. stored in an external file.

Other than the file= statement on <appSettings> (which is available only for app settings :-() it's not a "additional" setting - you just point your config system to an external file.

So you could have this in your app.config/web.config:

  <system.net>
    <mailSettings>
      <smtp configSource="smtp.test.config" />
    </mailSettings>
  </system.net>

and this in your smtp.test.config:

<?xml version="1.0" encoding="utf-8" ?>
<smtp>
  <network host="smtp.test.com" port="244" userName="test" password="secret" />
  <specifiedPickupDirectory pickupDirectoryLocation="C:\temp\mails"/>
</smtp>

This works as of .NET 2.0 (maybe even 1.x), and it works for every configuration section - but not for configuration section groups like <system.web>.

Of course, you can now create additional config files, like smtp.staging.config and so forth, and now your problem has been reduced to replacing a single line in your web.config.

You can do this using an installation script, a XML preprocessor, or even by human intervention.

It doesn't completely solve the problem as .NET 4 and the web.config transformations hopefully will, but it's a step and a bit of help.

marc_s
Okay. I thought this might be the solution (thank you for verifying it). I was hope I could just have two, full-fledged, configuration files and I could just tell a build or something which one to use.
JasCav
well, you can always define post-build steps, either in Visual Studio or a MSBuild environment, which would copy the appropriate config files to your output directory
marc_s
@marc_s - One (minor) problem I'm running into. I get a warning that "The 'smtp' element is not declared." and it's happening on the external files. Any thoughts or suggestions as to why this is happening?
JasCav
@Jason: that's just the Visual Studio intellisense that's not 100% up to snuff. VS doesn't fully know how to deal with these "partial" config files. But it works - sure does!
marc_s
@marc_s - Yup. :-) I saw it worked. I just don't like having warnings it my project. Ah well, guess there's not much I can do about that. Thanks again for your help, though!
JasCav