Creating custom variable in web.config file?

2019-02-05 13:02发布

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

3条回答
贼婆χ
2楼-- · 2019-02-05 13:34

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();
查看更多
Viruses.
3楼-- · 2019-02-05 13:55

in web.config:

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

in cs:

string str = ConfigurationManager.AppSettings["message"].ToString();
查看更多
干净又极端
4楼-- · 2019-02-05 13:55

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

查看更多
登录 后发表回答