繁体   English   中英

从web.config文件读取自定义标签

[英]reading custom tags from web.config file

我在web.config文件中创建了自定义标签,这些值存储在哈希表中。在哈希表中存储后,读取密钥和值都能够读取密钥但不读取值。这是我在Web中的代码。配置

<configSections>
    <sectionGroup name="BlockIpSection">
        <section name="ipslist" type="CustomConfigurationHandler.CustomConfiguration, CustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere"/>
    </sectionGroup>
</configSections>

<BlockIpSection>
    <ipslist>      
        <ipaddress1 value="100"></ipaddress1>
        <ipaddress2></ipaddress2>
    </ipslist>
</BlockIpSection>

这里有2个标签节组名和节名。我已经创建了标签为ipaddress1和ipaddress2.Am将值100存储到ipaddress1标签中。

我正在阅读这样的web.config

Hashtable config = (Hashtable)ConfigurationManager.GetSection("BlockIpSection/ipslist");
foreach (string key in config.Keys)
{ 
    Response.Write(key + "<br>");           
}

来自web.config的标记我们存储在哈希表中,并使用key读取。当我使用config [key]读取其给定值的值时。

尝试使用

App.BlockIpSection

要么

ConfigurationManager.AppSettings["BlockIpSection"]

首先,您需要在web.config中提及两个标签的 ,然后
使用NameValueCollection而不是Hashtable

哈希表的问题如下:

在添加手表上

试试下面的代码块:

NameValueCollection nvObj = new NameValueCollection();
nvObj = ConfigurationManager.GetSection("MyCustomSettings") as NameValueCollection;
Response.Write(hs["key1"] + "</br>"); 

在网络中定义键和值。 配置。 像这样 。

<BlockIpSection>
    <ipslist>      
        <add key="ipaddress1 " value="100"/>
        <add key="ipaddress2 " value="100"/>      
        // IP keys and values ....
    </ipslist>
</BlockIpSection>

Hashtable config = (Hashtable)ConfigurationManager.GetSection("BlockIpSection/ipslist");
foreach (DictionaryEntry dicEntry in config)
//OR
// foreach (KeyValuePair<string,string> dicEntry in config)
{
    string key = Convert.ToString(dicEntry.Key);
    string val = Convert.ToString(dicEntry.Value);
    // Your code goes here   
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM