Export (0) Print
Expand All

WebConfigurationManager.GetWebApplicationSection Method

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
)

Parameters

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.

ExceptionCondition
ConfigurationErrorsException

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.

NoteNote

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 TryCast function in Visual Basic.

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

NoteNote

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();
        }



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft