是否可以在asp.net web.config文件中创建自己的自定义键并使用C#进行迭代?你是如何做到的(我在哪里放钥匙?什么格式?)?我有一个Intranet应用程序,它根据客户端的IP地址执行某些操作
我的密钥将有一个名称,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; }