I want to create a variable in web.config file and to use that variable in web forms. How can I achieve this??
Asked
Active
Viewed 3.5k times
40
-
1yes. I got to know about
tag but nothing about creating variable. – Chakri Mar 07 '14 at 12:11 -
6lol, I had the same question and *did* go to google first. The first hit was this page! – SausageFingers Feb 13 '15 at 16:08
-
4Actually, this page is now the top result for a search - Kudos to Chakri! – Graham Laight Sep 25 '15 at 09:32
-
:p thanks, now I feel silly when I look at this question ;) – Chakri Oct 27 '15 at 15:18
3 Answers
75
in web.config:
<appSettings>
<add key="message" value="Hello, World!" />
</appSettings>
in cs:
string str = ConfigurationManager.AppSettings["message"].ToString();

user3256944 salutes Monica
- 774
- 1
- 12
- 27

DmitryK
- 1,333
- 1
- 11
- 18
-
1Please refer to this link http://stackoverflow.com/questions/1274852/the-name-configurationmanager-does-not-exist-in-the-current-context if you have issues with ConfigurationManager not working. – John M Jul 04 '16 at 14:17
-
1You forgot to mention to add the class in the code (`using System.Configuration`) – Malcolm Salvador Feb 03 '17 at 01:33
16
You may try like this:
<appSettings>
<add key="id" value="12762"/>
<add key ="url" value="http://localhost:10982/Redirect.aspx"/>
</appSettings>
Then you can use
using System.Configuration;
and use it like this:
string id=ConfigurationManager.AppSettings["Id"].ToString();
string url=ConfigurationManager.AppSettings["Url"].ToString();

Rahul Tripathi
- 168,305
- 31
- 280
- 331
4
FYI: The accepted answers for accessing web.config data are considered "Obsolete" now and should be replaced with:
Example:
in web.config:
<appSettings>
<add key="message" value="Hello, World!" />
</appSettings>
in c#:
ConfigurationManager.AppSettings["message"]
Reference: ConfigurationSettings.AppSettings is obsolete, warning

Community
- 1
- 1

AndyRBlank
- 106
- 8