gpt4 book ai didi

asp.net - 如何迭代 web.config 中的自定义键?

转载 作者:行者123 更新时间:2023-12-02 15:26:00 25 4
gpt4 key购买 nike

是否可以在 asp.net web.config 文件中创建我自己的自定义键并使用 C# 迭代它们?你如何做到这两点(我把 key 放在哪里?什么格式?)?我有一个 Intranet 应用程序,它根据客户端的 IP 地址执行某些操作。我认为我应该将它们放在 web.config 中并迭代它,而不是在代码隐藏文件中对它们进行硬编码。这样我就可以在配置文件中添加或删除内容,而无需重新编译所有内容。

我的 key 包含名称、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;
}

关于asp.net - 如何迭代 web.config 中的自定义键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/317591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com