简体   繁体   English

根据Web.config部分中的键读取值

[英]Read value based on key from a Web.config section

I am trying to create a function that can return the corresponding value from web.config based on the key provided 我正在尝试创建一个函数,该函数可以根据提供的密钥从web.config返回相应的值

For instance, I have these in my Web.config file: 例如,我的Web.config文件中有这些文件:

<Records>
    <add key="0" value="123cb456" />
    <add key="1" value="hFh343" />
    <add key="2" value="Hdkr625" />
    <add key="3" value="1732HHds" />
    <add key="4" value="optxy33" />
 </Records>

I am trying to create a function that can return the corresponding record value based on the key provided .Something like the following 我正在尝试创建一个函数,该函数可以根据提供的键返回相应的记录值。

 public static string GetSelectedRecordValue(string strkey)
 {
      string strValue;
      foreach (KeyValueConfigurationElement item in Configuration.Settings("Records")
      {
           if (item.Key == strKey)
           {
                strValue = item.Value;
                return strValue;
           }
      }
      return strValue;
 }

How to get the contents of my config section into a collection so that I can loop through each item and read correct value record based on a key ?any suggestions? 如何将我的config部分的内容放入一个集合中,以便我可以遍历每个项目并根据键读取正确的值记录?有什么建议吗?

Why You add this colors in webconfig? 为什么要在webconfig中添加此颜色? You can set these values in constant class . 您可以在常量 class设置这些值。 and you can get easily . 这样您就可以轻松获得。

Please refer the below websites 请参考以下网站

Link 1 链接1

Link 2 连结2

But if you want webconfig,then please see this same discussions 但是,如果您要使用webconfig,那么请参阅相同的讨论

Reading a key from the Web.Config using ConfigurationManager 使用ConfigurationManager从Web.Config读取密钥

How to read values from custom section in web.config 如何从web.config中的自定义部分读取值

I think you can try WebConfigurationManager.OpenWebConfiguration : http://msdn.microsoft.com/en-us/library/vstudio/system.web.configuration.webconfigurationmanager.openwebconfiguration(v=vs.90).aspx 我认为您可以尝试使用WebConfigurationManager.OpenWebConfiguration: http : //msdn.microsoft.com/zh-cn/library/vstudio/system.web.configuration.webconfigurationmanager.openwebconfiguration( v=vs.90) .aspx

or just read the web.config as a normal xml file. 或者只是将web.config读取为普通的xml文件。

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

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