Click here to Skip to main content
15,896,606 members
Articles / Web Development / ASP.NET

Creating Custom Configurations

Rate me:
Please Sign up or sign in to vote.
4.47/5 (18 votes)
2 Sep 200410 min read 81K   1.7K   70  
Create isolated settings, strongly-typed objects and collections inside your web.config by leveraging the flexibility of .NET to create your own configuration sections and handler.
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="index.aspx.vb" Inherits="ConfigurationSampleVB.WebForm1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>Sample Configuration</title>
    <style>
      html, body {background: #FFF;font-family: "helvetica", "arial", "verdana", sans-serif;font-size: 85%;line-height: 1.3;}
      body {font-size: 90%;}
      table {font-size:100%;}
    </style>
</HEAD>
  <body>	
    <form id="form" method="post" runat="server">
      <p align="center">
         <table border="0" width="550">
            <tr>
               <td>
                  <asp:DropDownList AutoPostBack="True" ID="list" Runat="server">
                     <asp:ListItem Value="0">Select...</asp:ListItem>
                     <asp:ListItem Value="1">Hard-coded (bad way #1)</asp:ListItem>
                     <asp:ListItem Value="2">Loop (bad way #2)</asp:ListItem>
                     <asp:ListItem Value="3">Strongly-typed (good way)</asp:ListItem>
                  </asp:DropDownList>
               </td>
            </tr>
            <tr>
               <td>&nbsp;</td>
            </tr>
            <tr>
               <td>
                  <asp:Repeater EnableViewState="False" ID="repeater" Runat="server">
                     <HeaderTemplate>
                        <h4>Files Copied From <asp:Literal ID="sourceServer" Runat="server" />, to:</h4>
                        <ul>
                     </HeaderTemplate>
                     <ItemTemplate> 
                        <li><%# DataBinder.Eval(Container.DataItem, "Address")%> &nbsp;&nbsp; [<%# DataBinder.Eval(Container.DataItem, "Name")%>]</li>
                     </ItemTemplate>
                     <FooterTemplate>
                        </ul>
                     </FooterTemplate>
                  </asp:Repeater>
               </td>
            </tr>
            <tr>
               <td>&nbsp;</td>
            </tr>
            <tr>
               <td>&nbsp;</td>
            </tr>            
            <tr>
               <td><h4 style="margin-bottom:0px;">Explanation</h4></td>
            </tr>
            <tr>
               <td>
                  The page has a dropdownlist with 3 choices, 2 bad ways to do things and 1 good way. Each method is in its own appropriately named function for you to quickly see what's going on, along with comments. The dropdownlist lets you select which method to take, all methods work and do the same thing (unless you start to change things in the web.config around, since the bad-methods aren't at all flexible)...so don't spend much time here, instead look at the code.<br /><br />
                  The requirements for this sample application is to copy files from a source server to any number of destination servers (we aren't actually doing any copying, it's all for show).<br /><br />
                  The first bad method looks for the hardcoded key's "server1", "serverName1", "server2", "serverName2" and so on in the appSetting section of the web.config.  Using this method we can't easily add new servers, and the page developer (you) needs to construct the server object manually each time you want to use it.<br /><br />
                  The second bad method creates the key's dynamically by looping an integer and doing a "server" + i join.  This method is a bit more flexible than the above one, but can easily be broken if your users don't follow your naming rules. Again you still don't have a strongly-type object.<br /><br />
                  Finally, the good way uses a custom section, custom handler and custom class.  It encapsulates all the parsing, supports richer syntax than the simple key=&gt;value pair and makes your life cake.                  
               </td>
            </tr>
         </table>
      </p>
    </form>
  </body>
</HTML>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions