Reading a variable from Web.Config

How can I add and read a value from the web.config file ?

+62
c # web-config
04 Oct '10 at 11:25
source share
6 answers

I would advise you not to change web.config, because every time it changes, it restarts your application.

However, you can read web.config using System.Configuration.ConfigurationManager.AppSettings

+52
04 Oct '10 at 11:33
source share

Given the following web.config file:

 <appSettings> <add key="ClientId" value="127605460617602"/> <add key="RedirectUrl" value="http://localhost:49548/Redirect.aspx"/> </appSettings> 

Usage example:

 using System.Configuration; string clientId = ConfigurationManager.AppSettings["ClientId"]; string redirectUrl = ConfigurationManager.AppSettings["RedirectUrl"]; 
+98
04 Oct '10 at 11:55
source share

If you need the basics, you can access the keys through:

 string myKey = System.Configuration.ConfigurationManager.AppSettings["myKey"].ToString(); string imageFolder = System.Configuration.ConfigurationManager.AppSettings["imageFolder"].ToString(); 

In order to access my keys for web configuration, I always make a static class in my application. This means that I can access them wherever I need, and I do not use strings throughout my application (if it changes in the web configuration, I will have to go through all the changes that they change). Here's a sample:

 using System.Configuration; public static class AppSettingsGet { public static string myKey { get { return ConfigurationManager.AppSettings["myKey"].ToString(); } } public static string imageFolder { get { return ConfigurationManager.AppSettings["imageFolder"].ToString(); } } // I also get my connection string from here public static string ConnectionString { get { return ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; } } } 
+14
Jun 01 '12 at 8:32
source share

Assuming the key is contained within the <appSettings> node:

 ConfigurationSettings.AppSettings["theKey"]; 

As for the "record" - just enter no.

Web.config is not intended for this, if you are going to constantly change the value, put it in a static helper class.

+6
04 Oct '10 at 11:32
source share

Ryan Farley has an excellent article on this on his blog, including all the reasons why you can't write back to web.config files: Writing to a .NET.NET configuration file

+3
04 Oct '10 at 11:33
source share

I am the SiteConfiguration class to call all my appSetting applications this way. I share this if it helps someone.

add the following code to the "web.config" file

 <configuration> <configSections> <!-- some stuff omitted here --> </configSections> <appSettings> <add key="appKeyString" value="abc" /> <add key="appKeyInt" value="123" /> </appSettings> </configuration> 

Now you can define a class to get all your appSetting value. like this

 using System; using System.Configuration; namespace Configuration { public static class SiteConfigurationReader { public static String appKeyString //for string type value { get { return ConfigurationManager.AppSettings.Get("appKeyString"); } } public static Int32 appKeyInt //to get integer value { get { return ConfigurationManager.AppSettings.Get("appKeyInt").ToInteger(true); } } // you can also get the app setting by passing the key public static Int32 GetAppSettingsInteger(string keyName) { try { return Convert.ToInt32(ConfigurationManager.AppSettings.Get(keyName)); } catch { return 0; } } } } 

Now add the link of the previous class and get access to the key call, as shown below:

 string appKeyStringVal= SiteConfigurationReader.appKeyString; int appKeyIntVal= SiteConfigurationReader.appKeyInt; int appKeyStringByPassingKey = SiteConfigurationReader.GetAppSettingsInteger("appKeyInt"); 
0
May 14 '16 at 19:56
source share



All Articles