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 4

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

Using the CustomItemHandler
Now you're ready to test the CustomItemHandler class. First, make sure your code compiles without errors. To test the class, you need to add the <section> tag that defines your custom handler for the <articlesVB> tag. Switch back to the ASP.NET project you started at the beginning of this article and add a reference to the CustomItemHandler.dll file created when you compiled the CustomItemHandler project. To create the reference, right-click on the References item in the Solution Explorer window and select Add Reference. Click the .NET tab and then click the Browse button. You'll find the DLL in the bin subdirectory of your CustomItemHandler project.

Next, make one more modification to your Web.config file. Within the <configSections> tag you created earlier, add a new <section> tag. Set the name attribute to "articlesVB" and set the type attribute to the class and assembly name of the handler you just created. At this point, the <configSections> section should look like this (your version numbers may differ):

   <configSections>
      <section name="customSystemItems" 
      type="System.Configuration.NameValueSectionHandler, 
      System, Version=1.0.3300.0, Culture=neutral, 
      PublicKeyToken=b77a5c561934e089"/>
      <section name="articlesVB" 
      type="CustomItemHandler.CustomTagHandler, 
      CustomItemHandler"/>
   </configSections>
WARNING: The Web.config file is XML and is therefore case sensitive! Make sure the attribute values match the case for both the tag and the assembly and class names.


Add the following code to your customItems.aspx Web Form to retrieve and display the articles as links:

   Dim articles As ArrayList
   Dim anArticleVB As CustomItemHandler.Article
   Dim o as Object
   Dim s As String
   Response.Write("<h3>ArticlesVB</h3>")
   articles = CType(System.Configuration. _
      ConfigurationSettings.GetConfig _
      ("articlesVB"), ArrayList)
      If Not articles Is Nothing Then
            For Each o In articles
             anArticleVB = CType(o,
              CustomItemHandler.Article)
            Response.Output.WriteLine _
               ("<a href=""" & anArticleVB.URL & _
               """" & ">" & anArticleVB.Title & _
              "</a>")
            If Not anArticleVB.Authors Is Nothing Then
               s = "by "
               For Each obj In anArticleVB.Authors
                  s += CType(obj, String) & ", "
               Next
               Response.Output.WriteLine _
                 (s.Substring(0, s.Length - 2))
            End If
         Next
      End If
Finally, compile and run the customItems.aspx Web Form. You'll see the header "ArticlesVB" followed by the list of article links defined by the <articles> section in the Web.config file (see Figure 2).

Figure 2. Web.config File: The list of links from the custom configuration section in the Web.config file.
You can follow the steps in this article to create custom handlers for any type of information that you can store in a configuration file. You've seen how to use the built-in <appSettings> section to read generic key/value settings, how to use System-defined handlers to read custom sections with system-defined attributes, and how to create and define custom handlers. Define your handlers in the machine.config file when you want to use custom sections across all ASP.NET applications on a particular server or define them in the web.config file when you want to create custom sections that apply to only a single application.



A. Russell Jones is the Executive Editor for DevX.
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