zoukankan      html  css  js  c++  java
  • [Config]如何利用ConfigurationSettings.AppSettings.GetValues读取配置文件中多个同Key的value

    编写者:郑昀@Ultrapower

    默认情况下,
    string[] strArray = System.Configuration.ConfigurationSettings.AppSettings.GetValues("Uri");
    是无法读取配置文件中多个同Key的value的。如下所示的配置:
    <appSettings>
      
      <add key="Uri" value="uri1"/>
      <add key="Uri" value="uri2"/>
      <add key="Uri" value="uri3"/>

     </appSettings>
    用MSDN告诉我们的GetValues是读不到的,只能读到最后一个value。
     
    只有这么做才可以:
    第一步:
    单独建立一个类库MultipleSectionHandler,把NameValueMultipleSectionHandler.cs加进去,并将MultipleSectionHandler.csproj加入到我们的工程中;
     
    第二步:
    编译MultipleSectionHandler,生成MultipleSectionHandler.dll;
     
    第三步:
    将WebApp应用的Web.config文件中加入
    <configSections>
      <remove name="appSettings" />
      <section name="appSettings" type="MyCompany.Configuration.NameValueMultipleSectionHandler, MultipleSectionHandler" />
     </configSections>
    表明对于appSettings的读取将采用我们自己的MultipleSectionHandler处理。
     
    第四步:
    这时候就可以针对Web.config中的:
    <appSettings>
      
      <add key="Uri" value="uri1"/>
      <add key="Uri" value="uri2"/>
      <add key="Uri" value="uri3"/>

     </appSettings>
    通过
    string[] strArray = System.Configuration.ConfigurationSettings.AppSettings.GetValues("Uri");
    来读取了
  • 相关阅读:
    Mybatis中#{}与${}的区别:
    JDBC
    JavaScript与jQuery的区别
    JavaScript 高级
    AdminLTE
    servlet过滤器与监听器
    数据库连接池
    大对象数据LOB的应用
    Serializable
    泛型
  • 原文地址:https://www.cnblogs.com/zhengyun_ustc/p/135815.html
Copyright © 2011-2022 走看看