Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Creating Custom Configuration Settings in ASP.NET  : Page 2

You don't have to limit yourself to the simple key/value settings available by default through the <AppSettings> section in the Web.config file. Instead, create custom configuration handlers to manage information however you like.


advertisement
   <section name="customSystemItems" type =    
      "System.Configuration.NameValueSectionHandler, 
      type="System.Configuration.NameValueSectionHandler, 
      System, Version=1.0.3300.0, Culture=neutral, 
      PublicKeyToken=b77a5c561934e089"/>
Author Note: The Version and PublicKeyToken values may differ on your version of the .NET framework. To find the appropriate values for your system, copy them from an existing <section> element.


2. Test the new <customSystemItems> tag by placing the new tag in your Web.config file, just before the closing </configuration> tag, for example:

   <configuration>
      <!&#151;the existing web.config settings here -->
      <customSystemItems>
         <add  key="SomeKey" value="SomeValue" />
      </customSystemItems>
   </configuration>
3. Save your changes to the web.config file. In your customItems.aspx Web Form, add the highlighted code to the Page_Load event:

   Dim aKey As String
   Response.Write("<h3>AppSettings</h3>")
   For Each aKey In ConfigurationSettings.AppSettings.Keys
      Response.Output.WriteLine(aKey & "=" & _
         ConfigurationSettings.AppSettings.Item(aKey))
   Next
   <font color="green">
   Response.Write("<h3>CustomSystemItems</h3>")
   For Each aKey In CType(ConfigurationSettings.GetConfig _
      ("customSystemItems"), _
      System.Collections.Specialized.NameValueCollection).Keys
      Response.Output.WriteLine(aKey & "=" & _
         ConfigurationSettings.AppSettings.Item(aKey))
   Next</font>
4. Now compile and run the Web Form again. This time, you'll see the CustomSystemItem header followed by the line "SomeKey=SomeValue", which corresponds to the single <add> child element you added to the <customSystemItem> element.

By modifying the machine.config file, you can use the defined custom tag in any Web application running on your server—but you may not always want the handler to apply to all applications. If you don't, you can add a <configSections> tag and the <section> tag to your web.config file instead. To test this, first remove the <section> tag you defined earlier from your machine.config file, and save the changes. Next, add a <configSections> tag immediately following the opening <configuration> tag in your web.config file, and place the <section> tag inside that, for example:



   <configuration>
      <configSections>    
         <section name="customSystemItems" type =    
         "System.Configuration.NameValueSectionHandler, 
         type="System.Configuration.NameValueSectionHandler, 
         System, Version=1.0.3300.0, Culture=neutral, 
         PublicKeyToken=b77a5c561934e089"/>
      </configSections>
      <!&#151;remainder of web.config file here -->
   </configuration>
5. So that you can see a change, append one more <add> tag to the <customSystemItems> section of your web.config file:

   <configuration>
      <!&#151;the existing web.config settings here -->
      <customSystemItems>
         <add  key="SomeKey" value="SomeValue" />
         <add  key="AnotherKey" value="AnotherValue" />
      </customSystemItems>
   </configuration>
Save the changes to your web.config file and run the customItems.aspx Web Form again. You will see two values now rather than one. You don't need to recompile your application to perform the test. ASP.NET applies configuration changes immediately.

You can define any number of custom tags in this manner; however, using the generic <add> tags and the key and value attribute names isn't particularly intuitive. For maintainability, it's more effective to create custom tags that control both the tag and attribute names.


Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date