How to define local variables web.config

I have a C # web application with .NET Framework 4.0 and I am trying to find a way to define a variable in Web.Config that can be referenced elsewhere in Web.Config.

I want something like this.

<?xml version="1.0" encoding="utf-8"?>
<LocalWebConfigVars>
    <add key="Var1" value="ServerName1"/>
    <add key="Var2" value="DatabaseName1"/>
</LocalWebConfigVars>
<configuration>
    <connectionStrings>
        <add name="AppConnectionString" connectionString="DATA SOURCE=@Var2 ServerName=@Var1"/>
        <add name="OtherStuff" value="@Var1"/>
etc...

      

Currently I need to keep 3 or 4 hardcoded values ​​(some built-in others only values) updated to the same and would like to make it easier to sync.

Is it possible?

Thank.

Edit:

Just some kind of background. The reason this becomes problematic is because we are defining an application database instance (among other specific parameters) in the web.config file. We have multiple instances of the database in our test and production environments, and if I have to switch to another while checking something and missing one of the hands full of links, I get some weird results. I try to avoid this by defining it once and referencing it everywhere.

+3


source to share


1 answer


Wouldn't just do the .config conversion for each of your environments, tweaking the configuration and project conversion, you could switch from environment to environment using the solution configuration dropdown.



See link for more information on conversions in .config files

+2


source







All Articles