WebConfigurationManager.GetSection 方法

定義

從目前 Web 應用程式的預設組態檔中擷取指定的組態區段。

多載

GetSection(String)

從目前 Web 應用程式的組態檔中擷取指定的組態區段。

GetSection(String, String)

在指定的位置從 Web 應用程式的組態檔擷取指定的組態區段。

GetSection(String)

從目前 Web 應用程式的組態檔中擷取指定的組態區段。

public:
 static System::Object ^ GetSection(System::String ^ sectionName);
public static object GetSection (string sectionName);
static member GetSection : string -> obj
Public Shared Function GetSection (sectionName As String) As Object

參數

sectionName
String

組態區段名稱。

傳回

Object

指定的組態區段物件;如果此區段不存在,則為 null。 請牢記,安全性限制在使用 GetSection(String) 時是當做執行階段作業。 例如,您可能無法在執行階段存取區段以進行修改。

例外狀況

無法載入有效的組態檔。

範例

本節中的範例示範如何使用 方法存取組 GetSection 態資訊。

下列範例顯示可從 Web 應用程式或主控台應用程式存取的區段。

注意

此範例示範如何使用 GetWebApplicationSection 方法從組態檔取得 ConfigurationSection 物件。


// Show how to use the GetSection(string). 
// to access the connectionStrings section.
static void GetConnectionStringsSection()
{

    // Get the connectionStrings section.
    ConnectionStringsSection connectionStringsSection =
        WebConfigurationManager.GetSection("connectionStrings")
        as ConnectionStringsSection;

    // Get the connectionStrings key,value pairs collection.
    ConnectionStringSettingsCollection connectionStrings =
        connectionStringsSection.ConnectionStrings;
   
    // Get the collection enumerator.
    IEnumerator connectionStringsEnum =
        connectionStrings.GetEnumerator();

    // Loop through the collection and 
    // display the connectionStrings key, value pairs.
    int i = 0;
    Console.WriteLine("[Display the connectionStrings]");
    while (connectionStringsEnum.MoveNext())
    {
        string name = connectionStrings[i].Name;
        Console.WriteLine("Name: {0} Value: {1}",
        name, connectionStrings[name]);
        i += 1;
    }

    Console.WriteLine();
}
' Show how to use the GetSection(string). 
' to access the connectionStrings section.
Shared Sub GetConnectionStringsSection()
   
   ' Get the connectionStrings section.
     Dim connectionStringsSection As ConnectionStringsSection = _
     WebConfigurationManager.GetSection("connectionStrings")
   
   ' Get the connectionStrings key,value pairs collection.
     Dim connectionStrings As ConnectionStringSettingsCollection = _
     connectionStringsSection.ConnectionStrings
   
   ' Get the collection enumerator.
     Dim connectionStringsEnum As IEnumerator = _
     connectionStrings.GetEnumerator()
   
   ' Loop through the collection and 
   ' display the connectionStrings key, value pairs.
   Dim i As Integer = 0
   Console.WriteLine("[Display the connectionStrings]")
   While connectionStringsEnum.MoveNext()
      Dim name As String = connectionStrings(i).Name
         Console.WriteLine("Name: {0} Value: {1}", _
         name, connectionStrings(name))
      i += 1
   End While
   
   Console.WriteLine()
End Sub

備註

如果 GetSection 從 Web 應用程式內呼叫 ,它會根據 Web 應用程式組態階層,從系統所選取的組態檔中取得 區段。

警告

如果您的應用程式使用與 HTTP 不同的通訊協定, GetSection 則會在其參數清單中接受區段名稱和路徑的多載是要使用的通訊協定。 您必須指定組態檔路徑,因為系統無法對組態階層層級進行任何假設。 如果您使用只接受區段名稱的多 GetSection 載,系統一律會嘗試傳回應用層級的組態設定。 不過請注意,如果指定的路徑不在目前應用程式之外,採用路徑的多載也會傳回目前執行中應用程式的應用層級組態設定。

您可以從用戶端應用程式內呼叫 GetSection 。 在此情況下,它會根據用戶端組態階層,從系統所選取的組態檔取得預設區段。 通常,這是Machine.config檔案,除非您已有對應的組態。 如需對應組態檔,請參閱接下來所述的對應方法。

注意

方法是 GetSection 執行時間方法,可在應用程式執行所在階層層級的組態檔區段上運作。 針對非執行時間作業,請改用 GetSection 。 這個方法會在組態檔的指定區段上運作,您可以使用其中一個多載方法來開啟組態檔 。 OpenWebConfiguration

給繼承者的注意事項

傳回值必須先轉換成預期的組態類型,才能使用。 若要避免可能的轉型例外狀況,您應該使用條件式轉換作業,例如 as C# 中的 運算子。

另請參閱

適用於

GetSection(String, String)

在指定的位置從 Web 應用程式的組態檔擷取指定的組態區段。

public:
 static System::Object ^ GetSection(System::String ^ sectionName, System::String ^ path);
public static object GetSection (string sectionName, string path);
static member GetSection : string * string -> obj
Public Shared Function GetSection (sectionName As String, path As String) As Object

參數

sectionName
String

組態區段名稱。

path
String

虛擬組態檔路徑。

傳回

Object

指定的組態區段物件;如果此區段不存在,則為 null。 務必記得,安全性限制在使用 GetSection(String, String) 時是當做執行階段作業。 例如,您可能無法在執行階段存取區段以進行修改。

例外狀況

這個方法是從 Web 應用程式外部呼叫。

無法載入有效的組態檔。

範例

下列範例示範如何使用 方法存取組 GetSection 態資訊。

注意

這個範例示範如何使用 GetSection 方法,從指定的組態檔取得 ConfigurationSection 物件。


// Show the use of GetSection(string, string). 
// to access the connectionStrings section.
 static void GetSection2()
 {

     try
     {
         // Get the connectionStrings section for the 
         // specified Web app. This GetSection overload
         // can olny be called from within a Web application.
         ConnectionStringsSection connectionStringsSection =
             WebConfigurationManager.GetSection("connectionStrings",
             "/configTest") as ConnectionStringsSection;

         // Get the connectionStrings key,value pairs collection
         ConnectionStringSettingsCollection connectionStrings =
             connectionStringsSection.ConnectionStrings;

         // Get the collection enumerator.
         IEnumerator connectionStringsEnum =
             connectionStrings.GetEnumerator();

         // Loop through the collection and 
         // display the connectionStrings key, value pairs.
         int i = 0;
         Console.WriteLine("[Display connectionStrings]");
         while (connectionStringsEnum.MoveNext())
         {
             string name = connectionStrings[i].Name;
             Console.WriteLine("Name: {0} Value: {1}",
             name, connectionStrings[name]);
             i += 1;
         }
         Console.WriteLine();
     }

     catch (InvalidOperationException e)
     {
         string errorMsg = e.ToString();
         Console.WriteLine(errorMsg);
     }
 }
' Show the use of GetSection(string, string). 
' to access the connectionStrings section.
Shared Sub GetSection2()
   
   Try
      ' Get the connectionStrings section for the 
      ' specified Web app. This GetSection overload
      ' can olny be called from within a Web application.
         Dim connectionStringsSection As ConnectionStringsSection = _
         WebConfigurationManager.GetSection( _
         "connectionStrings", "/configTest")
      
      ' Get the connectionStrings key,value pairs collection
         Dim connectionStrings As ConnectionStringSettingsCollection = _
         connectionStringsSection.ConnectionStrings
      
      ' Get the collection enumerator.
         Dim connectionStringsEnum As IEnumerator = _
         connectionStrings.GetEnumerator()
      
      ' Loop through the collection and 
      ' display the connectionStrings key, value pairs.
      Dim i As Integer = 0
      Console.WriteLine("[Display connectionStrings]")
      While connectionStringsEnum.MoveNext()
         Dim name As String = connectionStrings(i).Name
             Console.WriteLine("Name: {0} Value: {1}", _
             name, connectionStrings(name))
         i += 1
      End While
      Console.WriteLine()
   
   Catch e As InvalidOperationException
      Dim errorMsg As String = e.ToString()
      Console.WriteLine(errorMsg)
   End Try
End Sub

備註

如果 GetSection 從 Web 應用程式內呼叫 ,它會從組態階層中指定路徑所定義的組態檔中取得 區段。

警告

如果您的應用程式使用與 HTTP 不同的通訊協定, GetSection 則會在其參數清單中接受區段名稱和路徑的多載是要使用的通訊協定。 您必須指定組態檔路徑,因為系統無法對組態階層層級進行任何假設。 如果您使用只接受區段名稱的多 GetSection 載,系統一律會嘗試傳回應用層級的組態設定。 不過請注意,如果指定的路徑不在目前應用程式之外,採用路徑的多載也會傳回目前執行中應用程式的應用層級組態設定。

無法從用戶端應用程式內呼叫這個方法。

如果您想要從位於目前 Web 應用程式目錄層級的組態檔擷取組態區段,請使用 GetSection 方法。

注意

方法是 GetSection 在應用程式執行所在階層層級之組態檔區段上運作的執行時間方法。 針對非執行時間作業,請改用 GetSection 。 這個方法會在您使用其中 open 一個組態檔方法取得之組態檔的指定區段上運作。

給繼承者的注意事項

傳回值必須先轉換成預期的組態類型,才能使用。 若要避免可能的轉型例外狀況,您應該使用條件式轉換作業,例如 as C# 中的 運算子。

另請參閱

適用於