Use the deleettings property of the ConfigurationSettings object to retrieve ASP. NET configuration information.
ASP. NET allows developers to access configuration settings from applications by directly publishing configuration settings (in the form of strong type attributes) or using common configuration APIs. The following example shows how to use the Browser attribute of the System. Web. HttpRequest class to access the <browserCaps> Configuration section. This is a hash table about properties. These properties reflect the browser client function currently accessing the page. The actual <browserCaps> section data is included in the machine. config file.
The following is the program code: <% @ Page Language = "C #" %>
<Html>
<Body style = "font: 10.5pt ">
<H3> Search browser Boolean ActiveXControls = <% = Request. Browser. ActiveXControls. ToString () %> <br>
Boolean AOL = <% = Request. Browser. AOL. ToString () %> <br>
Boolean BackgroundSounds = <% = Request. Browser. BackgroundSounds. ToString () %> <br>
Boolean Beta = <% = Request. Browser. Beta. ToString () %> <br>
String Browser = <% = Request. Browser. Browser %> <br>
Boolean CDF = <% = Request. Browser. CDF. ToString () %> <br>
Boolean Cookies = <% = Request. Browser. Cookies. ToString () %> <br>
Boolean Crawler = <% = Request. Browser. Crawler. ToString () %> <br>
Boolean Frames = <% = Request. Browser. Frames. ToString () %> <br>
Boolean JavaApplets = <% = Request. Browser. JavaApplets. ToString () %> <br>
Boolean JavaScript = <% = Request. Browser. JavaScript. ToString () %> <br>
Int32 MajorVersion = <% = Request. Browser. MajorVersion. ToString () %> <br>
Double MinorVersion = <% = Request. Browser. MinorVersion. ToString () %> <br>
String Platform = <% = Request. Browser. Platform %> <br>
Boolean Tables = <% = Request. Browser. Tables. ToString () %> <br>
String Type = <% = Request. Browser. Type %> <br>
Boolean VBScript = <% = Request. Browser. VBScript. ToString () %> <br>
String Version = <% = Request. Browser. Version %> <br>
Boolean Win16 = <% = Request. Browser. Win16.ToString () %> <br>
Boolean Win32 = <% = Request. Browser. Win32.ToString () %> <br>
</Body>
</Html>
In addition to the access Configuration settings shown above, developers can also use the System. Configuration. ConfigurationSettings class to retrieve data in any Configuration section. Note that the specific objects returned by ConfigurationSettings depend on the section handler mapped to the configuration section (see IConfigurationSectionHandler. Create ). The following code shows how to access the configuration data exposed in the <customconfig> section. In this example, assume that the configuration section handler returns an object with the Enabled attribute and the type is mmconfigsettings.
The following is the program code: CustomConfigSettings config = (CustomConfigSettings) ConfigurationSettings ["customconfig"];
If (config. Enabled = true ){
// Do something here.
}
Dim config As CustomConfigSettings = CType (ConfigurationSettings ("customconfig"), CustomConfigSettings)
If config. Enabled = True Then
Do something here.
End If
Var config: CustomConfigSettings = CustomConfigSettings (ConfigurationSettings ["customconfig"]);
If (config. Enabled = true ){
// Do something here.
}
Use application settings
Configuration files are ideal for storing custom application settings, such as database connection strings, file paths, or remote XML Web Service URLs. The default configuration section (defined in the machine. config file) includes the <etettings> section, which can be used to store these settings as name/value pairs. The following example shows a configuration section, which defines the database connection string of the application.
<Configuration>
<Deleetask>
<Add key = "pubs" value = "server = (local) NetSDK; database = pubs; Trusted_Connection = yes"/>
<Add key = "northwind" value = "server = (local) NetSDK; database = northwind; Trusted_Connection = yes"/>
</AppSettings>
</Configuration>
The ConfigurationSettings object exposes a special configurettings attribute, which can be used to retrieve these settings:
The following is the program code: String dsn = ConfigurationSettings. deleettings ["pubs"];
Dim dsn As String = ConfigurationSettings. deleettings ("pubs ")
Var dsn: String = ConfigurationSettings. deleettings ["pubs"];
The following is the program code: <% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. SqlClient" %>
<% @ Import Namespace = "System. Configuration" %>
<Html>
<Script language = "C #" runat = "server">
Void Page_Load (Object Src, EventArgs E ){
String dsn = ConfigurationSettings. deleettings ["pubs"];
SqlConnection myConnection = new SqlConnection (dsn );
SqlDataAdapter myCommand = new SqlDataAdapter ("select * from Authors", myConnection );
DataSet ds = new DataSet ();
MyCommand. Fill (ds, "author ");
MyDataGrid. DataSource = new DataView (ds. Tables [0]);
MyDataGrid. DataBind ();
}
</Script>
<Body>
<H3> <font face = ""> retrieve configuration data </font> <ASP: DataGrid id = "MyDataGrid" runat = "server"
BackColor = "# ccccff"
BorderColor = "black"
ShowFooter = "false"
CellPadding = 3
CellSpacing = "0"
Font-Name = ""
Font-Size = "8pt"
HeaderStyle-BackColor = "# aaaadd"
/>
</Body>
</Html>