WebConfigurationManager.GetWebApplicationSection Method (String)


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

Namespace:   System.Web.Configuration
Assembly:  System.Web (in System.Web.dll)

public static object GetWebApplicationSection(
	string sectionName


Type: System.String

The configuration section name.

Return Value

Type: System.Object

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.

Exception Condition

A valid configuration file could not be loaded.

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.


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 Implementers:

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# or the function in Visual Basic.

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


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 =
         "connectionStrings") as ConnectionStringsSection;

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

     // Get the collection enumerator.
     IEnumerator connectionStringsEnum =

     // 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;


.NET Framework
Available since 2.0
Return to top