简体   繁体   English

如何使用DictionarySectionHandler在Web.Config中检索Key的值

[英]How to retrieve the value for the Key in the Web.Config using DictionarySectionHandler

How to fetch the corresponding value of a key that I would get dynamically. 如何获取我将动态获取的键的对应值。 I wish to use the system defined DictionarySectionHandler to do the job, of fetching the data from my custom built config section in the Web.config file 我希望使用系统定义的DictionarySectionHandler来完成从Web.config文件中的自定义构建的config部分中获取数据的工作。

Code block in Web.Config Web.Config中的代码块

<section name="domainsource" type="System.Configuration.DictionarySectionHandler"/>

  <domainSource>
         <add key="0" value="170" />
         <add key="1" value="171" />
         <add key="2" value="172" />
         <add key="3" value="173" />
         <add key="12" value="174" />
  </domainSource>

Sourcecode in the main cs file from where I wish to retrieve the data from the Web.Config 我要从中从Web.Config检索数据的主要CS文件中的源代码

Hashtable statusCodes = ConfigurationManager.GetSection("domainSource") as Hashtable;
vDomainSource = statusCodes[vDomainID];

This is where I am stuck vDomainID would be a value 0/1/2/3/12, based on this value I need to fetch its respective Source from the Web.Config. 在这里我被卡住了,vDomainID将是一个值0/1/2/3/12,基于此值,我需要从Web.Config中获取其各自的Source。 Any help on this aspect would be really appreciated. 在这方面的任何帮助将不胜感激。

You have a missspelling in the defintion of the section domainsource -> domainSource. 您对domainsource-> domainSource部分的定义有误。 Further ensure that the elemnt is defined in an element. 进一步确保要在元素中定义元素。 Then it should work. 然后它应该工作。

<configuration>
  <configSections>
  <section name="domainSource" type="System.Configuration.DictionarySectionHandler"/>
    </configSections>
  <domainSource>
    <add key="0" value="170" />
    <add key="1" value="171" />
    <add key="2" value="172" />
    <add key="3" value="173" />
    <add key="12" value="174" />
  </domainSource>
</configuration>

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

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