System/App Configuration in .NET

I mentioned I’m working with .NET stuff on my current project. One of the nicer features I’ve found so far is that .NET includes an entire package devoted to System/App Configuration. So I I’m writing an web application, I can put a web.config file in the root of my app and/or any of the subdirectories of my application. Inside that web.config file, I can set security, modify the compiler setting, specify an error handler… you get the picture. IMHO, the best part is that you can extend web.config and add your own custom settings. For instance, almost every web application worth a look uses some sort of database, which usually requires a database connection string or DSN name. You’d add something like this:

<appSettings>
   <add key=”connectionString” value=”user id=sa;password=mypassword;initial catalog=mydb;data source=mydbserver;Connect Timeout=30″ />
</appSettings>

outside of the system.web tag. To use that value in your application, you need not parse the xml file, you don’t have to worry about getting it into application scope, you don’t need to worry about locking. You only need to write one line of code:

private static String CONNECTIONSTRING = System.Configuration.ConfigurationSettings.AppSettings[“connectionString”].ToString();

Pretty simple isn’t it? It gets better. According to this document, “ASP.NET detects changes to configuration files and automatically applies new configuration settings to Web resources affected by the changes. The server does not have to be rebooted for the changes to take effect.” and more importantly, “ASP.NET protects configuration files from outside access by configuring Internet Information Services (IIS) to prevent direct browser access to configuration files.

Further Resources:

ASP.NET Configuration: [source]
Format of ASP.NET Configuration Files: [source]
Configuring Applications in .NET: [source]

5 thoughts on “System/App Configuration in .NET”

  1. We were discussing at work the best way to get Flash to listen to that file in our web apps because there was some tight stuff in there I could use. Rather than have Flash access the file directly and attempt to parse it, they figured, I guess for security, that they should write a web service wrapper, or even better, just include that data in the data access classes we already had setup.

    If you find a better way, or just something that works for you, please post!

  2. Correct me if I’m wrong – but isn’t this just a version of Application.cfm? (Not trying to start a CF-ASP war here – I know there are things that ASP does easily as well that CF does not.)

  3. Yeesh, that “add” syntax is awful… wouldn’t something like this be better?

    <appSettings>
      <connectionString>user id=sa;password=mypassword;initial catalog=mydb;data source=mydbserver;Connect Timeout=30</connectionString>
    </appSettings>

    or better yet,

    connectionString = user id=sa;password=mypassword;initial catalog=mydb;data source=mydbserver

  4. Hello,

    I am really confuse from slecting the right language to start with, which one is easier and more powerful is it ASP .NET or CFM.

    Thank you,

Leave a Reply

Your email address will not be published. Required fields are marked *