ASP.NET Tutorial: Using appSettings in web.config

ASP.NET Tutorial: Using appSettings in web.config
Hello Friends! welcome again on Time to Hack. In previous tutorial of ASP.NET we had used web.config to store and use our connection strings. Now in this tutorial we are going to take a look at appSettings section of web.config and use it to store various types of information and retrieve it when required.

The appSettings section of web.config will let you store any info in form of String as a key-value pair. For example the minimal appSettings section in web.config file is as follows:

  <add key="apiKey" value="95deb5011a8fe1ccf6552bb5bcda2ff0" />

AppSettings in Web.config file

And to read this key we will need to include System.Configuration namespace and the following code section in our program to use the above string:

string apiKey = ConfigurationSettings.AppSettings["apiKey"];

AppSettings in Code with Collections

Similarly following screenshot shows how it can be used directly in aspx page:

AppSettings use in aspx page

So all of the above code shows following output:

AppSettings Output

Now you can also put your configuration in separate file and import in the web.config file by following code

<appSettings file="relative path to file"> </appSettings>