Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables within app.config/web.config

Is it is possible to do something like the following in the app.config or web.config files?

<appSettings>  <add key="MyBaseDir" value="C:\MyBase" />  <add key="Dir1" value="[MyBaseDir]\Dir1"/>  <add key="Dir2" value="[MyBaseDir]\Dir2"/> </appSettings> 

I then want to access Dir2 in my code by simply saying:

 ConfigurationManager.AppSettings["Dir2"] 

This will help me when I install my application in different servers and locations wherein I will only have to change ONE entry in my entire app.config. (I know I can manage all the concatenation in code, but I prefer it this way).

like image 957
DeeStackOverflow Avatar asked Mar 02 '09 16:03

DeeStackOverflow


People also ask

How do you put environment variables in Web config?

For Applications, including Web Applications, On Windows: If you want environmental variables to be expanded your application will need to do that itself. A common way of doing this is to use the cmd syntax %variable% and then using Environment. ExpandEnvironmentVariables to expand them.

What Web config file contains?

web. config file is an XML-based configuration file used in ASP. NET-based applications to manage various settings that are concerned with the configuration of our website. In this way, we can separate our application logic from configuration logic.

Does Blazor use Web config?

config File To Host A Blazor App In IIS. Blazor projects, by default, do not contain web. config files. However, IIS cannot host a Blazor application without one.


2 Answers

A slightly more complicated, but far more flexible, alternative is to create a class that represents a configuration section. In your app.config / web.config file, you can have this:

<?xml version="1.0" encoding="utf-8" ?> <configuration>     <!-- This section must be the first section within the <configuration> node -->     <configSections>         <section name="DirectoryInfo" type="MyProjectNamespace.DirectoryInfoConfigSection, MyProjectAssemblyName" />     </configSections>      <DirectoryInfo>         <Directory MyBaseDir="C:\MyBase" Dir1="Dir1" Dir2="Dir2" />     </DirectoryInfo> </configuration> 

Then, in your .NET code (I'll use C# in my example), you can create two classes like this:

using System; using System.Configuration;  namespace MyProjectNamespace {      public class DirectoryInfoConfigSection : ConfigurationSection {          [ConfigurationProperty("Directory")]         public DirectoryConfigElement Directory {             get {                 return (DirectoryConfigElement)base["Directory"];             }     }      public class DirectoryConfigElement : ConfigurationElement {          [ConfigurationProperty("MyBaseDir")]         public String BaseDirectory {             get {                 return (String)base["MyBaseDir"];             }         }          [ConfigurationProperty("Dir1")]         public String Directory1 {             get {                 return (String)base["Dir1"];             }         }          [ConfigurationProperty("Dir2")]         public String Directory2 {             get {                 return (String)base["Dir2"];             }         }         // You can make custom properties to combine your directory names.         public String Directory1Resolved {             get {                 return System.IO.Path.Combine(BaseDirectory, Directory1);             }         }     } } 

Finally, in your program code, you can access your app.config variables, using your new classes, in this manner:

DirectoryInfoConfigSection config =   (DirectoryInfoConfigSection)ConfigurationManager.GetSection("DirectoryInfo"); String dir1Path = config.Directory.Directory1Resolved;  // This value will equal "C:\MyBase\Dir1" 
like image 147
Matt Hamsmith Avatar answered Oct 10 '22 09:10

Matt Hamsmith


You can accomplish using my library Expansive. Also available on nuget here.

It was designed with this as a primary use-case.

Moderate Example (using AppSettings as default source for token expansion)

In app.config:

<configuration>     <appSettings>         <add key="Domain" value="mycompany.com"/>         <add key="ServerName" value="db01.{Domain}"/>     </appSettings>     <connectionStrings>         <add name="Default" connectionString="server={ServerName};uid=uid;pwd=pwd;Initial Catalog=master;" provider="System.Data.SqlClient" />     </connectionStrings> </configuration> 

Use the .Expand() extension method on the string to be expanded:

var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString; connectionString.Expand() // returns "server=db01.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;" 

or

Use the Dynamic ConfigurationManager wrapper "Config" as follows (Explicit call to Expand() not necessary):

var serverName = Config.AppSettings.ServerName; // returns "db01.mycompany.com"  var connectionString = Config.ConnectionStrings.Default; // returns "server=db01.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;" 

Advanced Example 1 (using AppSettings as default source for token expansion)

In app.config:

<configuration>     <appSettings>         <add key="Environment" value="dev"/>         <add key="Domain" value="mycompany.com"/>         <add key="UserId" value="uid"/>         <add key="Password" value="pwd"/>         <add key="ServerName" value="db01-{Environment}.{Domain}"/>         <add key="ReportPath" value="\\{ServerName}\SomeFileShare"/>     </appSettings>     <connectionStrings>         <add name="Default" connectionString="server={ServerName};uid={UserId};pwd={Password};Initial Catalog=master;" provider="System.Data.SqlClient" />     </connectionStrings> </configuration> 

Use the .Expand() extension method on the string to be expanded:

var connectionString = ConfigurationManager.ConnectionStrings["Default"].ConnectionString; connectionString.Expand() // returns "server=db01-dev.mycompany.com;uid=uid;pwd=pwd;Initial Catalog=master;" 
like image 26
anderly Avatar answered Oct 10 '22 08:10

anderly