Rediger

Del via


WebConfigurationManager.GetWebApplicationSection(String) Method

Definition

Retrieves the specified configuration section from the current Web application's configuration file.

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

Parameters

sectionName
String

The configuration section name.

Returns

The specified configuration section object, or null if the section does not exist, or an internal object if the section is not accessible at run time.

Exceptions

A valid configuration file could not be loaded.

Examples

The following example shows how to access configuration information with the GetWebApplicationSection method.

Note

This example demonstrates how to use the GetWebApplicationSection method to get a ConfigurationSection object from the default configuration file.


// Show the use of GetWebApplicationSection(string).
// to get the connectionStrings section.
 static void GetWebApplicationSection()
 {

     // Get the default connectionStrings section,
     ConnectionStringsSection connectionStringsSection =
         WebConfigurationManager.GetWebApplicationSection(
         "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 connectionStrings]");
     while (connectionStringsEnum.MoveNext())
     {
         string name = connectionStrings[i].Name;
         Console.WriteLine("Name: {0} Value: {1}",
         name, connectionStrings[name]);
         i += 1;
     }

     Console.WriteLine();
 }
' Show the use of GetWebApplicationSection(string). 
' to access the connectionStrings section.
Shared Sub GetWebApplicationSection()
   
   ' Get the default connectionStrings section,
     Dim connectionStringsSection As ConnectionStringsSection = _
     WebConfigurationManager.GetWebApplicationSection( _
     "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 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

Remarks

If GetWebApplicationSection is called from within a Web application, it gets the section from the configuration file selected by the system according to the Web-application configuration hierarchy.

You could call GetWebApplicationSection from within a client application. In this case, it gets the default section from the configuration file selected by the system according to the client configuration hierarchy. Usually, this is the Machine.config file, unless you have a mapped configuration in place. For mapping configuration files, refer to the mapping methods described next.

Note

The GetWebApplicationSection method is a run-time operation that acts on the section of the application configuration file located at the current level. The GetSection method, however, is not a run-time operation but acts on the specified section obtained through one of the methods for opening the configuration files.

Notes to Inheritors

The return value must be cast to the expected configuration type before use. To avoid possible casting exceptions, you should use a conditional casting operation like the as operator in C#.

Applies to

See also