ASP.NETはWebを追加して読み取る.コンフィギュレーション・セクションのカスタマイズ

10700 ワード

カスタムセクション
1.まず、で、Index、testSectionなどのカスタム構成セクションと、対応するカスタム構成セクションハンドラ(NameValueSectionHandlerなど)を定義します.
2.次にセクションの内容を追加
<configuration>

  <configSections>

    <sectionGroup name="Rewrite.NET">

      <section name="Index" type="System.Configuration.NameValueSectionHandler" />

      <section name="SimpleSettingsMay1" type="System.Configuration.NameValueSectionHandler" />

      <section name="RegExpRule1" type="System.Configuration.NameValueSectionHandler" />

    </sectionGroup>

    

    <section name="testSection" type="System.Configuration.NameValueSectionHandler" />

  

  </configSections>

  <testSection>

    <add key="RegExpRule1" value="RexExpRule.RegExp,RexExpRule" />

    <add key="SimpleSettingsMay1" value="RulesEngine.SimpleRule,RulesEngine" />

  </testSection>

  <Rewrite.NET>

    <Index>

      <add key="RegExpRule1" value="RexExpRule.RegExp,RexExpRule" />

      <add key="SimpleSettingsMay1" value="RulesEngine.SimpleRule,RulesEngine" />

    </Index>



    <!--the actual settings for the rule set for the section-->

    <SimpleSettingsMay1>

      <add key="/rewrite.net/webform1.aspx?c=f" value="/rewrite.net/finalpage.aspx" />

    </SimpleSettingsMay1>



    <RegExpRule1>

      <add key="^/rewrite.net/corp/(.*)" value="/rewrite.net/about/$1" />

    </RegExpRule1>



  </Rewrite.NET>

。。。

</configuration>

3.コードで読み取る
NameValueCollection SectionIndex = (NameValueCollection)ConfigurationManager.GetSection("Rewrite.NET/Index");
NameValueCollection SectionIndex = (NameValueCollection)ConfigurationManager.GetSection("testSection");

 
カスタムコンフィギュレーション・セクションのハンドラは、カスタム化することもできます.
1.カスタムクラスの作成
 
public class MyHandler : ConfigurationSection

    {

        [ConfigurationProperty("myAttrib1", DefaultValue = "Clowns", IsRequired = true)]

        [StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]

        public String MyAttrib1

        {

            get

            { return (String)this["myAttrib1"]; }

            set

            { this["myAttrib1"] = value; }

        }



    }

2.に対応関係を登録する
  <configSections>

      <section 

        name="myCustomSection" 

        type="MyConfigSectionHandler.MyHandler, MyCustomConfigurationHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" 

        allowLocation="true" 

        allowDefinition="Everywhere"

      />

      <!-- Other <section> and <sectionGroup> elements. -->

  </configSections>
<myCustomSection myAttrib1="Clowns">

 3.読み取り
MyConfigSectionHandler.MyHandler config =

            (MyConfigSectionHandler.MyHandler)System.Configuration.ConfigurationManager.GetSection(

            "myCustomSection");

 
 
 
msdn https://msdn.microsoft.com/zh-cn/library/2tw134k3(v=vs.80).aspx