如何读取web.config中的自定义节的值 [英] How to read values from custom section in web.config

查看:136
本文介绍了如何读取web.config中的自定义节的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在web.config文件中得到了下面的示例code。

I have got below sample code in web.config file.

    <configuration>
      <configSections>
        <section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </configSections>
<secureAppSettings>
        <add key="userName" value="username"/>
        <add key="userPassword" value="password"/>
    </secureAppSettings>  
    </configuration>

我的新栏目 secureAppSettings 解密并有它里面的两个键。

My new section secureAppSettings is decrypted and is having two keys inside it.

现在在我的code,我要访问这些关键类似如下:

Now in my code, I want to access these key something like below:

string userName = System.Configuration.ConfigurationManager.secureAppSettings["userName"];
string userPassword = System.Configuration.ConfigurationManager.secureAppSettings["userPassword"];

但它返回为这些字段。

我怎样才能得到的值?

推荐答案

您可以访问它们作为键/值对:

You could access them as key/value pairs:

NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("secureAppSettings");
string userName = section["userName"];
string userPassword = section["userPassword"];

这篇关于如何读取web.config中的自定义节的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆