Silverlight Tips provides simple and useful tutorials and tips with real life examples, live demos and sample codes to download.
About authors:
Damon Serji
Damon Serji,
Silverlight developer working at Metia in London.
Gavin Wignall
Gavin Wignall,
Interactive Design Lead, working in design for over 10 years, the last 3 being in Silverlight.
Allan Muller
Allan Muller,
Developer, working on various types of Silverlight and WCF projects.

You could use a variable globally in your .NET website if you declare it in your Web.config file.

in your Web.config file find <appSettings> tag and insert your variables before it's closing tag:

<appSettings>
  <add key="MyVariable1" value="False" />
  <add key="MyName" value="Damon Serji" />
</appSetting>

Now you can easily access this variable and its value from your C# code:

string myVariable = System.Configuration.ConfigurationSettings.AppSettings["MyName"];

To break the above code down and to make sense of it: System really looks in your Web.Config file and the rest of the line becomes more obvious when you look at your Web.Config structure:


<?xml version="1.0" encoding="UTF-8"?>
  <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
      <configSections>
      .
      .
      </configSections>     
      .
     <appSettings>
       <add key="MyVariable1" value="False" />
       <add key="MyName" value="Damon Serji" />
     </appSetting>
     .
     .

So your Web.Config is nothing but a simple XML. System.Configuration tells the compiler to look in to <configuration> tag and System.Configuration.ConfigurationSettings.AppSettings tells it specifically to look into the <appSettings> tag. The result is a simple array of other tags which were added by you manually, so to find a specific tag you need to give it the index of the array i.e. an integer, or the "key" for that array, which is another way to find the tag you are looking for.

Posted by Damon Serji on 22. September 2009 18:06 under: Intermediate
 with 1 Comments