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.
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.
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:
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.
<add key="Hello" value="World" />
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:
<add key="Membership" value="false" />
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.
<compilation debug="false" />
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.
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.