3

I have a web application in C# with .NET Framework 4.0 and I'm trying to find a way to define a variable in Web.Config that can be referenced elsewhere within the 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 have to keep 3 or 4 hard-coded values (some embedded others just the value) updated to the same thing and would like to make it easier to keep in sync.

Is this possible?

Thanks.

Edit:

Just some background. The reason this is becoming problematic is that we define the apps database instance (among other instance specific setting) in the web.config. We have multiple database instances in our test and production environments and if I need to switch to a different one them while testing something and miss one of the hand full of references I get some strange results. I'm trying to avoid this by defining it once and referencing it everywhere else.

Dan
  • 2,369
  • 6
  • 36
  • 50
  • how come you can't create a Custom Config Class and inside the class assign Property values that point to each of your keys..? or even simpler use `ConfigurationManager.AppSettings` to access the properties.. do not hard code it's easier to add to the .config than to hard code and keep recompiling code for simple changes.. – MethodMan Nov 05 '14 at 17:54
  • I'm not sure what you're suggesting. To elaborate on my situation we deploy multiple instances of the same web application to different servers and point them to different database servers in both production and test environments. The web.config is being used to tell the specific instance of the app where to look for those things. So in summary each instance of our application has a unique web.config that tells it where it's specific database and server are. – Dan Nov 05 '14 at 17:58
  • @DJKRAZE We do use `ConfigurationManager.AppSettings` within the app to look at web.config variables like the connection string in my example but I was hoping for a self contained web.config syntax that would allow me to change a name in 1 place and all dependent strings update using the references new value. – Dan Nov 05 '14 at 18:02
  • 1
    Like I said you may want to create a custom class that holds AppSettings and Config Settings here look at this post http://stackoverflow.com/questions/603009/variables-within-app-config-web-config – MethodMan Nov 05 '14 at 18:13
  • 1
    Maybe you should consider using T4 templates http://msdn.microsoft.com/en-us/library/bb126445.aspx http://dukelupus.wordpress.com/2013/09/26/t4-templates-for-web-configapp-config-appsettings/ – Jason Evans Nov 05 '14 at 18:25

1 Answers1

2

Would not doing a simply .config transformation for each of your environments work, by setting up a project configuration & transform, you would be able to swap from environment to environment by the use of solutions configuration dropdown.

See this link for more info on transforms on .config files

CodeX
  • 71
  • 6
  • This seems reasonable as a solution but I have limitations in my work environment where I can only use approved tools. This solution appears to require a third party utility to run against the .config file to fill in the variables. Does something like this exist within Visual Studios or .NET? – Dan Nov 05 '14 at 19:38
  • web.config transforms are done via MSBuild, they are native to Visual Studio from V2010 on. Another good article is [Link](http://www.asp.net/mvc/overview/deployment/visual-studio-web-deployment/web-config-transformations) – CodeX Nov 05 '14 at 20:12
  • Does this transformation only occur during a build for deployment or can I use this syntax while doing debug work in visual studios too? – Dan Nov 05 '14 at 20:30
  • I've not done so, but see this [link](http://www.kongsli.net/2012/01/13/enabling-web-transforms-when-debugging-asp-net-apps/) – CodeX Nov 05 '14 at 21:50