3

I am part of a 10 person team working on an ASP.NET web application, but am the only developer in a satellite office (which is on a different domain than the main office). In the Web.config, I need to make alterations to have the web application work on my specific domain (alterations to the <identity> element and to database connection strings) that are different than everyone else on my team uses.

I can't alter the original Web.config file, since that would mess up everyone else on my team. The MsBuild Web.config transformations looked promising, but only work if you deploy the web application (I'm just pointing IIS at my application's root directory). I explored manually running a <TransformXml> build step, but it won't overwrite my local Web.config file (Access is Denied error)

What can I do to easily make a few selective changes to my local Web.config file without modifying what's in source control?

Mike
  • 5,560
  • 12
  • 41
  • 52

2 Answers2

1

The most common sections to require local changes are connectionStrings and appSettings. For both of these sections, the .NET framework allows for storage in a separate file.

You then simply keep these separate file out of source control (ignore, get once then cloak, disable versioning on file... whatever flavour source control you use).

For example,

<configuration>
    <connectionStrings configSource="connections.config"/>
</configuration>


<connectionStrings>
    <add name="Name"  
     providerName="System.Data.ProviderName" 
     connectionString="My;Personal;Connection;String;" />
</connectionStrings>
Paul-Jan
  • 16,746
  • 1
  • 63
  • 95
  • What about the element for process impersonation? (something that can only go in the Web.config) – Mike Feb 10 '11 at 19:04
0

I don't know of any solutions that don't modify what's in Source Control. But here are some options you can consider that might work in your situation:

Global.asax conditional settings:

void Session_Start(object sender, EventArgs e)
{
  if (HttpContext.Current.Server.MachineName == "JOE"  ){
    //my development machine
    Application["mysetting"] = "myspecial setting"; 
  }else{
    // ... 
  }
}

Multiple web.configs

Sometimes you have different web.configs for different servers. Managing the settings in each can become a nightmare, so a question on stack overflow (can't find it! sorry) suggested this, where the proper web.config.<setting> is copy-pasted into the main web.config file as appropriate.

Application/
  web.config.devserver
  web.config.joebrown
  web.config.production

Change your web.config settings dynamically

See a post on Stack Overflow for more details: asp.net managing multiple web.config files

public static string GetConnString()
{
    string connString = ConfigurationSettings.AppSettings[GetConfigKey("database")];
    return connString;
}

public static string GetConfigKey(string baseKey)
{
    string str = baseKey;
    if (Dns.GetHostName().StartsWith("dinoch"))
    {
        str = str + "-dev";
    }
    else if (Dns.GetHostName().StartsWith("prodsrvr"))
    {
        str = str + "-prod";
    }
    return str;
}

<configuration>
   <appSettings>
     <add key="database-dev" value="server=(local)\vsdotnet;database=ASPXAPPS;Integrated Security=SSPI" />
     <add key="database-prod" value="server=(local)\vsdotnet;database=ASPXAPPS;Integrated Security=SSPI" />
   </appSettings>
</configuration>
Community
  • 1
  • 1
rlb.usa
  • 14,942
  • 16
  • 80
  • 128