Sams Teach Yourself .Net in 21 Days
- Table of Contents
- Copyright
- About the Author
- About the Technical Editor
- Acknowledgments
- We Want to Hear from You
- Introduction
- Week 1: At a Glance
- Day 1. Introduction to the Microsoft .NET Framework
- Day 2. Introduction to Visual Studio .NET
- Day 3. Writing Windows Forms Applications
- Day 4. Deploying Windows Forms Applications
- Day 5. Writing ASP.NET Applications
- Introduction to ASP.NET
- Hello ASP.NET!
- Using Validation Controls
- Managing State in ASP.NET Web Applications
- Understanding the Web.Config File
- Summary
- Q&A
- Quiz
- Exercises
- Day 6. Deploying ASP.NET Applications
- Day 7. Exceptions, Debugging, and Tracing
- Week 1. In Review
- Week 2: At a Glance
- Day 8. Core Language Concepts in Visual Basic .NET and C#
- Day 9. Using Namespaces in .NET
- Day 10. Accessing Data with ADO.NET
- Day 11. Understanding Visual Database Tools
- Day 12. Accessing XML in .NET
- Day 13. XML Web Services in .NET
- Day 14. Components and .NET
- Week 2. In Review
- Week 3: At a Glance
- Day 15. Writing International Applications
- Day 16. Using Macros in Visual Studio .NET
- Day 17. Automating Visual Studio .NET
- Day 18. Using Crystal Reports
- Day 19. Understanding Microsoft Application Center Test
- Day 20. Using Visual SourceSafe
- Day 21. Object Role Modeling with Visio
- Week 3. In Review
Understanding the Web.Config File
The Web.Config file is an XML-based configuration file that's used by ASP.NET to set options for your application. Each new project you create has its own Web.Config file. Within a project, you can have multiple Web.Config files in different folders, setting up a hierarchy of what settings to use for each folder.
When you're just getting your feet wet with ASP.NET, you'll normally just use the one default Web.Config file on a per-project basis.
As you've seen in the previous section, you can use the Web.Config file to set application-level settings for state management. You can also set security on directories, set up page-and session-level debugging, and store your own custom configuration information.
The biggest benefit to the Web.Config file is that you have a place to store variable data that might have otherwise been kept in application-level or session-level variables in ASP. For example, database connection information, folder locations, and virtual path information can all be stored in the Web.Config file. With an ASPX page, you can retrieve information from the Web.Config file by using the ConfigurationSettings class.
To see how this works, take a look at the custom AppSettings section in the following Web.Config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="cn" value="Server=Enterprise;UID=NCC1701D;
Pwd=CaptJaneway;Database=Ships"/>
<add key="pics" value="C:\InetPub\storage\"/>
<add key="picsHTTP" value="http://www.picsserver.com/public/"/>
</appSettings>
<system.web>
Each of the values stored can be accessed by retrieving the correct key attribute in code. To do this, you would use the following code:
sqlCn = ConfigurationSettings.AppSettings("cn")
You could also use the GetSettings method to return an array of the items in the AppSettings collection. Storing variable data in the Web.Config file is important for two reasons:
- You don't need to recompile your application if a setting changes; you simply modify the Web.Config file.
- You aren't consuming memory using session- or application-level variables.
Summary | Next Section

Account Sign In
View your cart