WebConfigurationManager.GetSection 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從目前網頁應用程式的預設設定檔擷取指定的設定區段。
多載
| 名稱 | Description |
|---|---|
| GetSection(String) |
從目前網頁應用程式的設定檔中擷取指定的設定區段。 |
| GetSection(String, String) |
從網頁應用程式設定檔中,在指定位置擷取指定的設定區段。 |
GetSection(String)
從目前網頁應用程式的設定檔中擷取指定的設定區段。
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
設定區段名稱。
傳回
指定的設定區段物件,或 null 是該區段不存在。 請記得,作為 GetSection(String) 執行時操作使用受到安全限制。 例如,你可能無法在執行時存取某個區段進行修改。
例外狀況
無法載入有效的設定檔。
範例
本節範例展示了如何利用該 GetSection 方法存取設定資訊。
以下範例展示了一個可從網頁應用程式或主控台應用程式存取的區段。
Note
這個範例示範如何利用此 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 從網頁應用程式內部呼叫,系統會依照網頁應用程式設定階層選擇的設定檔區段。
注意事項
如果你的應用程式使用與 HTTP 不同的協定, GetSection 那麼會同時佔用區段名稱和參數清單路徑的超載。 您必須指定設定檔路徑,因為系統無法對組態階層層級做出任何假設。 如果你使用只包含區段名稱的 GetSection 超載,系統總是會嘗試在應用程式層級回傳設定。 不過請注意,如果其指定的路徑位於目前應用程式之外,該路徑的超載也會回傳目前執行應用程式的應用程式層級設定。
你可以在客戶端應用程式內呼叫 GetSection 。 在這種情況下,它會從系統依據用戶端設定階層所選擇的設定檔中取得預設區段。 通常,這是 Machine.config 檔案,除非你有映射設定。 關於映射設定檔,請參考接下來描述的映射方法。
Note
此 GetSection 方法是一種執行時方法,會在應用程式執行的階層層級上操作設定檔的區域。 非執行時操作則使用 GetSection 。 此方法作用於你使用其中一種超載方式開啟設定檔所取得的指定區段。 OpenWebConfiguration
給繼承者的注意事項
回傳值必須在使用前轉換為預期的配置類型。 為了避免可能的投射異常,你應該使用條件投射操作,就像 C# 裡的 as 運算子一樣。
另請參閱
適用於
GetSection(String, String)
從網頁應用程式設定檔中,在指定位置擷取指定的設定區段。
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
虛擬設定檔路徑。
傳回
指定的設定區段物件,或 null 是該區段不存在。 請記得,作為 GetSection(String, String) 執行時操作使用受到安全限制。 例如,你可能無法在執行時存取某個區段進行修改。
例外狀況
此方法可從網頁應用程式外部呼叫。
無法載入有效的設定檔。
範例
以下範例說明如何利用該 GetSection 方法存取設定資訊。
Note
此範例示範如何使用此 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 從網頁應用程式內部呼叫,則會取得由設定階層中指定路徑所定義的設定檔區段。
注意事項
如果你的應用程式使用與 HTTP 不同的協定, GetSection 那麼會同時佔用區段名稱和參數清單路徑的超載。 您必須指定設定檔路徑,因為系統無法對組態階層層級做出任何假設。 如果你使用只包含區段名稱的 GetSection 超載,系統總是會嘗試在應用程式層級回傳設定。 不過請注意,如果其指定的路徑位於目前應用程式之外,該路徑的超載也會回傳目前執行應用程式的應用程式層級設定。
此方法無法從用戶端應用程式內部呼叫。
如果你想從目前網頁應用程式目錄層級的設定檔擷取設定區段,請使用此 GetSection 方法。
Note
此 GetSection 方法是一種執行時方法,操作於應用程式執行的階層層級設定檔區段。 非執行時操作則使用 GetSection 。 此方法操作於你使用 open 其中一個設定檔方法取得的指定區段。
給繼承者的注意事項
回傳值必須在使用前轉換為預期的配置類型。 為了避免可能的投射異常,你應該使用條件投射操作,就像 C# 裡的 as 運算子一樣。