Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating custom variable in web.config file?

I want to create a variable in web.config file and to use that variable in web forms. How can I achieve this??

like image 682
Chakri Avatar asked Mar 07 '14 12:03

Chakri


3 Answers

in web.config:

<appSettings>
   <add key="message" value="Hello, World!" />
</appSettings> 

in cs:

string str = ConfigurationManager.AppSettings["message"].ToString();
like image 147
DmitryK Avatar answered Nov 15 '22 01:11

DmitryK


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();
like image 34
Rahul Tripathi Avatar answered Nov 15 '22 02:11

Rahul Tripathi


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

like image 4
AndyRBlank Avatar answered Nov 15 '22 02:11

AndyRBlank