Retrieve Information/ConnectionString from Web.Config file in Asp.Net

Leave a Comment

Asp.Net provides the WebConfigurationManager class in the System.Web.Configuration namespace, which allows you to extract information from a configuration file at runtime.

The WebConfigurationManager class gives convinient access to two configuration sections: the <appSettings> section, where you can define custom settings, and the <connectionStrings> section, used to define how your application connects to the database. You can get this information using the AppSettings and ConnectionStrings properties.

Using the WebConfigurationManager.GetSection() method, you can retrieve information about any other configuration section.

Retrieve the <authentication> element inside the <system.web> element.
AuthenticationSection authSection = (Authentication)WebConfigurationManager.GetSection("system.web/authentication");

Displays information about the assemblies that are currently referenced:
CompilationSection compSection = (CompilationSection)WebConfigurationManager.GetSection(@"system.web/compilation");
foreach (AssemblyInfo assm in compSection.Assemblies)
{
Response.Write(assm.Assembly + "<br /");
}

Retrieve information about connection string -
foreach (ConnectionStringSettings connection in
WebConfigurationManager.ConnectionStrings)
{
Response.Write("Name: " + connection.Name + "<br />");
Response.Write("Connection String: " +
 connection.ConnectionString + "<br /><br />");
}

0 comments:

Post a Comment