如何在web.config中迭代自定义键以实现动态配置管理?

2026-03-30 11:491阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计422个文字,预计阅读时间需要2分钟。

如何在web.config中迭代自定义键以实现动态配置管理?

可以在asp.net的web.config文件中创建自定义键,并通过C#代码读取使用。以下是一个简化的示例:

xml

在C#代码中,可以通过以下方式读取:

csharpstring value=ConfigurationManager.AppSettings[MyCustomKey];Console.WriteLine(value);

是否可以在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>

这是你的代码……

如何在web.config中迭代自定义键以实现动态配置管理?

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; }

本文共计422个文字,预计阅读时间需要2分钟。

如何在web.config中迭代自定义键以实现动态配置管理?

可以在asp.net的web.config文件中创建自定义键,并通过C#代码读取使用。以下是一个简化的示例:

xml

在C#代码中,可以通过以下方式读取:

csharpstring value=ConfigurationManager.AppSettings[MyCustomKey];Console.WriteLine(value);

是否可以在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>

这是你的代码……

如何在web.config中迭代自定义键以实现动态配置管理?

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; }