WebConfigurationManager.GetSection 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从当前 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
配置节名称。
返回
指定的配置节对象,或者 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 应用程序配置层次结构从系统选择的配置文件中获取该部分。
Caution
如果应用程序使用与 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
虚拟配置文件路径。
返回
指定的配置节对象,或者 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 应用程序中调用,它将从配置层次结构中的指定路径定义的配置文件中获取该节。
Caution
如果应用程序使用与 HTTP 不同的协议,则 GetSection 采用节名称和参数列表中的路径的重载是要使用的。 必须指定配置文件路径,因为系统无法对配置层次结构级别做出任何假设。 如果使用仅采用节名称的 GetSection 重载,系统将始终尝试在应用程序级别返回配置设置。 但请注意,如果指定的路径不在当前应用程序之外,则采用路径的重载也将返回当前正在运行的应用程序的应用程序级配置设置。
不能从客户端应用程序内部调用此方法。
如果要从位于当前 Web 应用程序目录级别的配置文件中检索配置节,请使用 GetSection 该方法。
注释
该方法 GetSection 是在运行应用程序的层次结构级别的配置文件部分中运行的运行时方法。 对于非运行时操作,请改用 GetSection 。 此方法对使用 open 某个配置文件方法获取的配置文件的指定节进行操作。
继承者说明
返回值必须在使用前强制转换为预期的配置类型。 为了避免可能的强制转换异常,应使用条件强制转换操作,如 as C# 中的运算符。