Creating custom variable in web.config file?

岁酱吖の 提交于 2019-11-29 04:41:17

问题


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


回答1:


in web.config:

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

in cs:

string str = ConfigurationManager.AppSettings["message"].ToString();



回答2:


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();



回答3:


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



来源:https://stackoverflow.com/questions/22249672/creating-custom-variable-in-web-config-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!