Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read appSettings section in the web.config file?

My XML looks like this and the filename is web.config

<?xml version="1.0"?> <configuration>   <appSettings>        <add key="configFile" value="IIS.config"/>     <add key="RialtoDomain" value="ASNC_AUDITORS"/>       </appSettings>   <system.serviceModel>     ....   </system.serviceModel> </configuration> 

In the code when I read like this

String path = ConfigurationSettings.AppSettings["configFile"]; 

I am getting a null value. No exception is thrown. Is this the right way to do it?

like image 824
realn Avatar asked Nov 16 '11 05:11

realn


People also ask

What is the AppSettings section in the web config file?

AppSetting section in the configuration file is a section that allows us to keep configurable and application wide settings (for e.g.: ConnectionString) that an application requires in order to perform the tasks properly. This helps in easy maintenance and deployment of the application.

How do I access AppSettings in C#?

To access these values, there is one static class named ConfigurationManager, which has one getter property named AppSettings. We can just pass the key inside the AppSettings and get the desired value from AppSettings section, as shown below.


1 Answers

Since you're accessing a web.config you should probably use

using System.Web.Configuration;  WebConfigurationManager.AppSettings["configFile"] 
like image 124
marc_s Avatar answered Sep 25 '22 17:09

marc_s