0

I have an entry in my <appSettings> section which contains my connection string. By default, it looks for the file on the root of the C: drive.

  <appSettings>
      <add key="EnvironmentFile" value="C:\environment.extension" />
  </appSettings>

I need to modify the value of the entry programmatically since the app will be deployed to a different server (Appharbor), which I don't have access to the C drive. I moved the environment file to the root of the app directory which means I need to modify the appSettings value for EnvironmentFile. Here's what I have tried so far, I have resorted to the following since I can't put relative values on web.config such as

value="~/environment.extension"

Default.aspx:

protected void Page_Load(object sender, EventArgs e)
{
        if(ConfigurationManager.AppSettings["EnvironmentFile"] == null)
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
            config.AppSettings.Settings.Add("EnvironmentFile", AppDomain.CurrentDomain.BaseDirectory + @"environment.extension");

            config.Save(ConfigurationSaveMode.Minimal);
            ConfigurationManager.RefreshSection("appSettings");
        }

        if (IsPostBack == false)
        {
            //--some business logic here that gets data from the database
        }
}

In debug mode, it does modify my web.config but throws an exception since it is already executing the code on my if block. Are there other ways to do this such that I have different configuration for debug and release?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jmc
  • 1,649
  • 6
  • 26
  • 47
  • Or you could use different web.config files depending on your environment... – Laurent S. Mar 11 '15 at 09:50
  • maybe having different web.config files could solve the issue but the application is quite big and contains a lot of configurations. might be tedious to maintain 2 files – jmc Mar 11 '15 at 09:57
  • I think [this stackoverflow question](http://stackoverflow.com/questions/305447/using-different-web-config-in-development-and-production-environment) might be of help to you – Laurent S. Mar 11 '15 at 10:01

1 Answers1

0

Yes, there is another way of doing this.

If you right click on your Web.Config in Visual Studio you'll see an option to 'Add Config Transform'. This allows you to create transformations for your various configurations. The newly added Web.XXX.config files will have some examples of how to transform your entries, but a simple example would be:

In your base Web.Config:

  <appSettings>
      <add key="Mode" value="Development" />
  </appSettings>

And then in a WebConfig.LiveRelease.config file you might have this:

<appSettings>
    <add key="Mode" value="Live" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/>
</appSettings>
Mashton
  • 6,037
  • 2
  • 25
  • 35
  • hi, i tried this solution but as I mentioned above, I can't write a relative path to my environment file. My config and environment files are on the same level. I tried the following: 1.) value="environment.extension" 2.) value="~/environment.extension" 3.) value="~\environment.extension" 4.) value="./environment.extension" 5.) value=".\environment.extension" 6.) value="MyApp\environment.extension" – jmc Mar 11 '15 at 10:41
  • But why have the connection string in a file referenced by the web.config rather than just in the web.config itself, and then transform that string based on your configuration? – Mashton Mar 11 '15 at 10:44
  • we are using an orm for our data access layer. the environment.extension is the configuration file used by the orm for generating classes thus the application references the same file. – jmc Mar 11 '15 at 10:50
  • But then again I will be going back to specifying the relative path to my environment file on e.g .\EnvironmentSpecificConfigurations\dev.config. Any thoughts? – jmc Mar 11 '15 at 11:03
  • But that combined with the transforms should help shouldn't it? So your different web.XXX.configs have different relative paths specified, so that your `EnvironmentKey` value is transformed based on your configuration. Absolute paths are bad anyway with webstuff, so if you can express it as relative in your solution it will be more straightforward. – Mashton Mar 11 '15 at 11:37
  • I don't have issues with specifying relative paths for my web.XXX.configs. As I have stated in my question and my first comment, the problem roots to writing a relative path on web.config. Please also see samples that I have tried. – jmc Mar 12 '15 at 06:34
  • This is where I bow out as I am completely failing to understand your problem - sorry. I thought you were trying to change the value of `EnvironmentKey` based on the configuration (e.g. debug, release etc) and were trying to do it in code. I showed that you can do it with web transforms and forget the code. – Mashton Mar 12 '15 at 09:48
  • Putting my self in your shoes, my question also has a little confusion in the sense that it could lead to a solution which produces another question - my apologies also as we are lured into the X Y problem (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). I'm looking forward to using your solution once I figure out how relative paths work within web.config :) – jmc Mar 12 '15 at 10:04
  • I have somewhat found a workaround for this issue. Using a test page, I was able to get the path to my application on the deployment server using AppDomain.CurrentDomain.BaseDirectory and then using the web.config transform as you suggested. But it gave me another problem, I've created a separate thread for this question here http://stackoverflow.com/questions/29026561/appharbor-getting-the-path-to-a-config-file-in-the-deployment-server – jmc Mar 13 '15 at 07:08