<區段>元素

包含組態區段宣告。

<組態>
  <configSections>
    <區段>

<組態>
  <configSections>
    <sectionGroup>
      <區段>

Syntax

<section name="section name"
         type="configuration section handler class, assembly"
         allowDefinition="Everywhere|MachineOnly|MachineToApplication"
         allowLocation="true|false" />

必要的屬性

描述
name 指定組態區段的名稱。
type 指定從組態檔讀取區段的組態區段處理常式類別名稱。 型別值具有語法「fully-qualified-section-handler-class-name、simple-assembly-name」。 簡單組建名稱是根檔案名,不含 .dll 副檔名。

選用屬性

下列屬性僅適用于 ASP.NET 應用程式。 組態系統會忽略其他應用程式類型的這些屬性。

描述
allowDefinition 指定區段可以使用的組態檔。 請使用下列其中一個值:

所有位置
允許在任何組態檔中使用區段。 此為預設值。
MachineOnly
只允許在電腦群組態檔中使用區段 (Machine.config)。
MachineToApplication
允許在機器組態檔或應用程式組態檔中使用區段。
allowLocation 判斷區段是否可以在 <location> 元素內使用。 請使用下列其中一個值:

true
允許在 <location> 元素內使用區段。 此為預設值。
false
不允許在 <location> 元素內使用區段。

父元素

描述
<configSections> 元素 包含組態區段和命名空間宣告。
<sectionGroup> 元素 定義組態區段的命名空間。

注意

<section> 元素是 <configSections><sectionGroup> 的子元素,但不是兩者共有。

子元素

備註

宣告組態區段基本上會定義組態檔的新元素。 新元素包含組態區段處理常式 (也就是實作 IConfigurationSectionHandler 介的類別) 讀取的設定。 您定義的區段屬性和子項目取決於您用來讀取設定的區段處理常式。

Machine.config 檔案中宣告組態區段處理常式,可讓您在該電腦上的任何應用程式組態檔中使用組態區段,除非 allowDefinition 屬性另有指定。

範例

下列範例示範如何定義組態區段,並定義該區段的設定:

<configuration>
  <configSections>
    <section name="sampleSection"
             type="System.Configuration.SingleTagSectionHandler"
             allowLocation="false" />
  </configSections>
  <sampleSection setting1="Value1"
                 setting2="value two"
                 setting3="third value" />
</configuration>

組態檔

應用程式組態檔、電腦組態檔 (Machine.config) 與不在應用程式目錄層級的 Web.config 檔案中,皆可使用此元素。

另請參閱