Beginnermediate ASP.NET

ASP.NET for newbs to intermediate

Web.Config Basics pt1

One things that has always been utterly confusing in ASP.NET is the dang Web.Config file.  For the beginner to ASP.NET it makes next to no sense and Visual Studio adds a whole bunch of stuff to it that I have no idea what it does.  It is also so odd and confusing if I want to do basic config stuff in my application I have no idea how in the world to go about it. 

Partly out of necessity and partly to rid myself of not knowing how the web.config works I have been spending time learning about it.  I am first starting out with the basics.  Getting something to work.

Getting Started

With most things I want to learn I completely bypass the "how" at first and get something basic to work, which is usually a "Hello World".  That is what we are going to work on in this post.  Lets just see something work.  So these are the two objectives:

1) Set a configuration option we can use in our code
2) Allow for debugging our ASP.NET application

Now, we don't "need" a web.config to run basic .aspx pages, but if you want to anything more advanced than a one page site then you will need one.

Web.Config

Here is our web.config that we are going to look at today.

This is really all we need to set a configuration option to access in code and to allow for debugging our application.  Please notice that everything is wrapped in:

<configuration>
</configuration>

That is important to note because all your config information should be under the root configuration node.

Custom Application Configuration

Next we have where we will set configuration options.

<appSettings>
     <add key="Hello" value="World" />
</appSettings>

In the appSettings is where you will put configuration options that are like global variables to your WHOLE asp.net application.  So in this case you will access the Hello key and it will return the world as its value.  So the code you would use to access the config options is:

You will use ConfigurationManager object which access all the information in your web.config file.  Since the Hello key is in the <appSettings> node then you would use AppSettings collection to access what key value pair you need.  In this case accessing hello and it returns world.

Why would you ever need to use this? Well one things comes to mind.  You might have a membership system that you want to turn on and off so you could have in the web.config:

<appSettings>
    <add key="Membership" value="false" />
</appSettings>

In your code you might have it check to see if it is true or false if true then it displays login stuff for people to register and login if false it doesn't display it. It might look something like:

If(ConfigurationManager.AppSettings["Membership"] == "true")
    //display login control

This is just a possibility of something you might want to do.

Debug

<system.web>
    <compilation debug="false" />
</system.web>

Here we set whether we want to debug our application or not.  I usually set this to yes as I have yet to write a bunch of code with out a bug.  All you need to do is to change false to true and you will enable debugging in your asp.net application.

Conclusion

The web.config file is actually a little more simple than it appears once you understand more of what is going on with it.  If you take learning it slowly and one little step at a time it is quite possible.  I hope to cover a little bit at a time of the web.config as I learn more and more since it is quite important to learn.

Comments

fanfreak said:

i developed a web site with some google help.  however i didn't understand why i used configurationmanager.  now i understood.  thanks

# August 17, 2009 8:18 PM

Red85 said:

Coupled with the above challenge is how we represent agents in space. ,

# October 22, 2009 6:08 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)