Access app.config in ASP.NET

I am using the app.config file to read data from it. I load the app.config file as:

string app_path = HttpContext.Current.Server.MapPath("app.config"); xmlDoc.Load(app_path); string image_path = ConfigurationManager.AppSettings["Test1"]; 

and I want to get the value "Test1". But the value of test1 starts with "null". how can I get the value "test1" from the app.config file .. I created the app.config file as:

 <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="Test1" value="My value 1" /> <add key="Test2" value="Another value 2" /> </appSettings> </configuration> 

please help me.

+4
source share
3 answers

In ASP.NET applications, the default configuration file is called web.config. This is an agreement that you should probably adhere to, which allows you to easily use the ConfigurationManager to access configuration settings.

I suggest taking a look at http://en.wikipedia.org/wiki/Web.config as a starting point to learn how to get into the main configuration of a .NET application in ASP.NET.

You can link configuration files together by setting the file attribute of the configuration sections that you want to override: http://www.codeproject.com/KB/dotnet/appsettings_fileattribute.aspx

+5
source

Web.config:

 <?xml version="1.0" encoding="utf-8"?> <configuration> <appSettings> <add key="Test1" value="My value 1" /> <add key="Test2" value="Another value 2" /> </appSettings> </configuration> 

code:

 string image_path = ConfigurationManager.AppSettings["Test1"]; 
+10
source

If you want to use ConfigurationManager.AppSettings inside a web application, you must put your AppSettings section in web.config.

0
source

All Articles