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.
• Table of Contents
• Index
Sams Teach Yourself Visual Studio® .NET 2003 in 21 Days
By
Jason Beres
Publisher
: Sams Publishing
Pub Date
: January 14, 2003
ISBN
: 0-672-32421-0
Pages
: 696
Sams Teach Yourself Visual Studio .NET in 21 Days will help developers that are new to application development and experienced developers understand how to use the .NET Framework and Visual Studio .NET to rapidly develop any type of computer application. The Visual Studio .NET development environment is the most comprehensive developer tool ever created, putting that together with the .NET Frameworks' Class Libraries, the developer has everything he or she needs to get up-to-speed on Microsoft's latest revolution in application development. This book will guide the developer through using the VS .NET IDE, the Visual Basic .NET and C# language, and the supporting tools available from Microsoft to create Windows and Web-based applications. The market is full of books that pretty much say the same thing, which is already available in the help files, the author of this book has written and deployed over a dozen successful applications using Visual Studio .NET and the .NET Framework. All of his expertise and experience is used to give you the most comprehensive title on using Visual Studio .NET.