• ASP.NET Configuration PART 2

    Posted on May 31, 2012 by in ASP.NET, Dotnet, VB.NET

    ASP.NET allows developers to access configuration settings from within an application either by exposing configuration settings directly (as strongly typed properties) or by using general configuration APIs. The following sample shows a page that accesses the <browserCaps> configuration section using the Browser property of the System.Web.HttpRequest class. This is a hash table of attributes that reflect the capabilities of the browser client that is currently accessing the page. The actual <browserCaps> section data is included in the machine.config file.

    <html>
    <div>
            <h3>Retrieving Browser Capabilities</h3> 
    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>
        </div>
    </body>
    </html>
    

    In addition to accessing configuration settings, as demonstrated above, developers also can use the System.Configuration.ConfigurationSettings class to retrieve the data for any arbitrary configuration section. Note that the particular object returned by ConfigurationSettings depends on the section handler mapped to the configuration section (see IConfigurationsectionHandler.Create). The following code demonstrates how you can access the configuration data exposed for a <customconfig> section. In this example, it is assumed that the configuration section handler returns an object of type CustomConfigSettings with the property Enabled.

    Dim config As CustomConfigSettings = CType(ConfigurationSettings(" customconfig"), CustomConfigSettings)
            If config.Enabled = True Then
                ' Do something here.
            End If
    

    Using Application Settings
    Configuration files are perfectly suited for storing custom application settings, such as database connection strings, file paths, or remote Web Service URLs. The default configuration sections (defined in the machine.config file) include an <appSettings> section that may be used to store these settings as name/value pairs. The following example shows an <appSettings> configuration section that defines database connection strings for an application.

    <configuration>
    <appSettings>
        <add key= "pubs" value="server= (local) \NetSDK; database=pubs;Trusted_Connection=yes " />
        <add key="northwind" value="server= (local) \NetSDK; database=pubs ; Trusted_Connect:ion=yes " />
      </appSettings>
    </configuration>
    

    The ConfigurationSettings object exposes a special AppSettings property that can be used to retrieve these settings:

    Dim dsn As String = ConfigurationManager.AppSettings("pubs").ToString()
    Be Sociable, Share!
      Post Tagged with ,

    Written by

    Software architect with over 10 years of proven experience in designing & developing n-tier and web based software applications, for Finance, Telecommunication, Manufacturing, Internet and other Commercial industries. He believes that success depends on one's ability to integrate multiple technologies to solve a simple as well as complicated problem.

    View all articles by

    Email : [email protected]

    Leave a Reply