如何在web.config文件中实现asp.net自定义键的循环遍历?
- 内容介绍
- 文章标签
- 相关推荐
本文共计432个文字,预计阅读时间需要2分钟。
在ASP.NET的web.config文件中,可以创建自定义键。使用C#,你可以直接在配置文件中添加如下内容:
xml
然后,在C#代码中,你可以这样访问这个自定义键:
csharpstring value=Configuration.AppSettings[MyCustomKey];
是否可以在asp.net web.config文件中创建自己的自定义键并使用C#进行迭代?你是如何做到的(我在哪里放钥匙?什么格式?)?我有一个Intranet应用程序,它根据客户端的IP地址执行某些操作.我没想在代码隐藏文件中对它们进行硬编码,而是将它们放在web.config中并迭代它.这样我就可以在配置文件中添加或删除而无需重新编译所有内容.我的密钥将有一个名称,IP地址,也许还有其他信息.
谢谢.
我认为这应该为你做…这是在你的web.config中…
<configSections> <section name="DataBaseKeys" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </configSections> <DataBaseKeys> <!--Connection Strings for databases (or IP Addresses or whatever)--> <add key="dbCon1" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/> <add key="dbCon2" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/> <add key="dbCon3" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/> <add key="dbCon4" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/> <add key="dbCon5" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/> </DataBaseKeys>
这是你的代码……
using System.Configuration; using System.Collections.Specialized; protected void Page_Load(object sender, EventArgs e) { LoadDdls(); } private void LoadDdls() { NameValueCollection nvcDbKeys = GetDbKeys(); //Loop through the collection for (int i = 0; i < nvcDbKeys.Count; i++) { // "Keys" is the "key" - Get(int) is the "value" this.DropDownList1.Items.Add(new ListItem(nvcDbKeys.Keys[i], nvcDbKeys.Get(i))); } } private NameValueCollection GetDbKeys() { //Declare a name value collection to store Database Key List from web.config NameValueCollection nvcDatabaseKeyList; nvcDatabaseKeyList = (NameValueCollection) ConfigurationManager.GetSection("DataBaseKeys"); return nvcDatabaseKeyList; }
本文共计432个文字,预计阅读时间需要2分钟。
在ASP.NET的web.config文件中,可以创建自定义键。使用C#,你可以直接在配置文件中添加如下内容:
xml
然后,在C#代码中,你可以这样访问这个自定义键:
csharpstring value=Configuration.AppSettings[MyCustomKey];
是否可以在asp.net web.config文件中创建自己的自定义键并使用C#进行迭代?你是如何做到的(我在哪里放钥匙?什么格式?)?我有一个Intranet应用程序,它根据客户端的IP地址执行某些操作.我没想在代码隐藏文件中对它们进行硬编码,而是将它们放在web.config中并迭代它.这样我就可以在配置文件中添加或删除而无需重新编译所有内容.我的密钥将有一个名称,IP地址,也许还有其他信息.
谢谢.
我认为这应该为你做…这是在你的web.config中…
<configSections> <section name="DataBaseKeys" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> </configSections> <DataBaseKeys> <!--Connection Strings for databases (or IP Addresses or whatever)--> <add key="dbCon1" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/> <add key="dbCon2" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/> <add key="dbCon3" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/> <add key="dbCon4" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/> <add key="dbCon5" value="Data Source=DbServerPath;Integrated Security=True;database=DbName1"/> </DataBaseKeys>
这是你的代码……
using System.Configuration; using System.Collections.Specialized; protected void Page_Load(object sender, EventArgs e) { LoadDdls(); } private void LoadDdls() { NameValueCollection nvcDbKeys = GetDbKeys(); //Loop through the collection for (int i = 0; i < nvcDbKeys.Count; i++) { // "Keys" is the "key" - Get(int) is the "value" this.DropDownList1.Items.Add(new ListItem(nvcDbKeys.Keys[i], nvcDbKeys.Get(i))); } } private NameValueCollection GetDbKeys() { //Declare a name value collection to store Database Key List from web.config NameValueCollection nvcDatabaseKeyList; nvcDatabaseKeyList = (NameValueCollection) ConfigurationManager.GetSection("DataBaseKeys"); return nvcDatabaseKeyList; }

