home *** CD-ROM | disk | FTP | other *** search
Wrap
<%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%> <!-- #include virtual="/quickstart/aspplus/include/header.inc" --> <h4>Retrieving Configuration</h4> <p> ASP+ allows developers to access configuration settings from within an application by either exposing configuration settings directly (as strongly-typed properties) or via general configuration APIs. The first sample below shows a page which accesses the <browsercaps> configuration section using the <b>Browser</b> property of the System.Web.UI.Page class. This is a dictionary of attributes that reflect the capabilities of the browser client which is currently accessing the page. The actual browsercaps section data is included in the machine-level config.web file. <p> <Acme:SourceRef RunSample="/quickstart/aspplus/samples/config/browscaps.aspx" ViewSource="/quickstart/aspplus/samples/config/browscaps.src" Icon="/quickstart/aspplus/images/browscaps.gif" Caption="BrowsCaps.aspx" runat="server" /> <p> In addition to accessing configuration as demonstrated above, developers may also use the <b>GetConfig</b> method of the System.Web.HttpContext class to retrieve the data for any arbitrary configuration section. Note that the particular object returned by GetConfig is dependent on the SectionHandler mapped to the configuration section (see IConfigurationSectionHandler::Create). The below code demonstrates how a developer can access the configuration data exposed for a "customconfig" section. In this example, we assume the configuration section handler returns an Object of type "CustomConfigSettings" with a property "Enabled". <div class="code"><pre> CustomConfigSettings config = (CustomConfigSettings) Context.GetConfig("customconfig"); if (config.Enabled == true) { // do something here... } </pre></div> <p> The below code demonstrates how a developer could access the "customconfig" configuration data exposed for a specific URL: <p> <div class="code"><pre> CustomConfigSettings config = (CustomConfigSettings) Context.GetConfig("customconfig", "/myapp/test.aspx"); if (config.Enabled == true) { // do something here... } </pre></div> <b>Important:</b> Since the configuration settings might change at any time (for example: someone updates a config.web file), a developer should not attempt to cache any configuration settings themselves รป and should instead rely solely on the configuration system to do this. <p> <h5>Defining a Custom Configuration Section</h5> Configuration files are perfectly suited for storing custom application settings, such as database connection strings, file paths, or remote Web Service URLs. ASP+ provides a convenient configuration section handler, <b>System.Web.Configuration.DictionarySectionHandler</b>, which can be mapped to a section of your choosing. For example, the following config.web file maps the DictionarySectionHandler to a section named "databases", which is used to store connection strings. <p> <div class="code"><pre> <configuration> <configsections> <add name="databases" type="System.Web.Configuration.DictionarySectionHandler" /> </configsections> <databases> <add key="pubs" value="server=localhost;uid=sa;pwd=;database=pubs" /> <add key="northwind" value="server=localhost;uid=sa;pwd=;database=pubs" /> </databases> </configuration> </pre></div> <p> The DictionarySectionHandler returns a <b>Hashtable</b> containing the key/value pairs defined in the databases section. From within application code, you can access this configuration data using the GetConfig method of the HttpContext: <p> <div class="code"><pre> String dsn = (String) ((Hashtable) Context.GetConfig("databases"))["pubs"]; </pre></div> <p> The following example shows the above code in action: <p> <Acme:SourceRef RunSample="/quickstart/aspplus/samples/config/config1.aspx" ViewSource="/quickstart/aspplus/samples/config/config1.src" Icon="/quickstart/aspplus/images/config.gif" Caption="Config1.aspx" runat="server" /> <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->