home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / aspplus / doc / configretrieve.aspx < prev    next >
Encoding:
Text File  |  2000-06-11  |  4.2 KB  |  99 lines

  1. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  2.  
  3. <!-- #include virtual="/quickstart/aspplus/include/header.inc" -->
  4.  
  5. <h4>Retrieving Configuration</h4>
  6.  
  7. <p>
  8. ASP+ allows developers to access configuration settings from within an application by either exposing configuration 
  9. settings directly (as strongly-typed properties) or via general configuration APIs.  The first sample below shows a page
  10. which accesses the <browsercaps> configuration section using the <b>Browser</b> property of the System.Web.UI.Page 
  11. class.  This is a dictionary of attributes that reflect the capabilities of the browser client which is currently accessing the page.
  12. The actual browsercaps section data is included in the machine-level config.web file.
  13. <p>
  14.  
  15. <Acme:SourceRef 
  16.   RunSample="/quickstart/aspplus/samples/config/browscaps.aspx" 
  17.   ViewSource="/quickstart/aspplus/samples/config/browscaps.src"
  18.   Icon="/quickstart/aspplus/images/browscaps.gif"
  19.   Caption="BrowsCaps.aspx"
  20.   runat="server" />
  21.  
  22. <p>
  23. In addition to accessing configuration as demonstrated above, developers may also use the <b>GetConfig</b> method of 
  24. the System.Web.HttpContext class to retrieve the data for any arbitrary configuration section.  
  25. Note that the particular object returned by GetConfig is dependent on the SectionHandler mapped to the 
  26. configuration section (see IConfigurationSectionHandler::Create).
  27. The below code demonstrates how a developer can access the configuration data exposed for a "customconfig" section.  In this example, we 
  28. assume the configuration section handler returns an Object of type "CustomConfigSettings" with a property "Enabled".
  29.  
  30. <div class="code"><pre>
  31. CustomConfigSettings config = (CustomConfigSettings) Context.GetConfig("customconfig");
  32.  
  33. if (config.Enabled == true)  
  34. {
  35.     // do something here...
  36. }
  37. </pre></div>
  38.  
  39. <p>
  40. The below code demonstrates how a developer could access the "customconfig" configuration data exposed for a specific URL:
  41. <p>
  42.  
  43. <div class="code"><pre>
  44. CustomConfigSettings config = (CustomConfigSettings) Context.GetConfig("customconfig", "/myapp/test.aspx");
  45.  
  46. if (config.Enabled == true)  
  47. {
  48.     // do something here...
  49. }
  50. </pre></div>
  51.  
  52. <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.
  53. <p>
  54.  
  55. <h5>Defining a Custom Configuration Section</h5>
  56.  
  57. Configuration files are perfectly suited for storing custom application settings, such as database connection strings, file paths, or remote
  58. Web Service URLs.  ASP+ provides a convenient configuration section handler, <b>System.Web.Configuration.DictionarySectionHandler</b>,
  59. which can be mapped to a section of your choosing.  For example, the following config.web file maps the DictionarySectionHandler to a
  60. section named "databases", which is used to store connection strings.
  61. <p>
  62.  
  63. <div class="code"><pre>
  64. <configuration>
  65.  
  66.     <configsections>
  67.         <add name="databases" type="System.Web.Configuration.DictionarySectionHandler" />
  68.     </configsections>
  69.  
  70.     <databases>
  71.         <add key="pubs" value="server=localhost;uid=sa;pwd=;database=pubs" />
  72.         <add key="northwind" value="server=localhost;uid=sa;pwd=;database=pubs" />
  73.     </databases>
  74.     
  75. </configuration>
  76. </pre></div>
  77.  
  78. <p>
  79. The DictionarySectionHandler returns a <b>Hashtable</b> containing the key/value pairs defined in the databases section.  From within 
  80. application code, you can access this configuration data using the GetConfig method of the HttpContext:
  81. <p>
  82.  
  83. <div class="code"><pre>
  84. String dsn = (String) ((Hashtable) Context.GetConfig("databases"))["pubs"];
  85. </pre></div>
  86.  
  87. <p>
  88. The following example shows the above code in action:
  89. <p>
  90.  
  91. <Acme:SourceRef 
  92.   RunSample="/quickstart/aspplus/samples/config/config1.aspx" 
  93.   ViewSource="/quickstart/aspplus/samples/config/config1.src"
  94.   Icon="/quickstart/aspplus/images/config.gif"
  95.   Caption="Config1.aspx"
  96.   runat="server" />
  97.  
  98.  
  99. <!-- #include virtual="/quickstart/aspplus/include/footer.inc" -->