DotNet Tutorials

Server Intellect

 Introduction to Using the Web.config (Overview)

Learn the behind the scenes work of the Web.config file. The Web.config file is an xml file that stores several server settings to process a request. It can be used to store machine-wide variables, direct requested extensions to use specific handlers, page security, etc.

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

If you open a new Web Application in Visual Studio 2008, you will notice Visual Studio automatically creates a Web.config file for you. This file can be one of several Web.config files on your machine. Others can be located in the .NET framework library, server root, and even subdirectories. All parent Web.config file settings will be inherited by child Web.config files.  All child Web.config files can overwrite the settings listed in their parent Web.config files unless you specify the setting to not be overwritten.  The Web.config file in your current working directory will have the most dominate settings.

All settings are inside of <configuration> xml tags. The Web.config file is an xml file which must be declared at the top of the document. Below is an example of that.

<?xml version="1.0"?>

<configuration>
     <system.web>
          <!— sections-->    
     </system.web>
     <appSettings>
          <!— sections -->    
     </appSettings >
</configuration>

As you can see in the example above, I've included the 2 most common child nodes of the <configuration> tag. The <system.web> child and the <appSettings> child. Like their names indicate, the <system.web> tag contains elements that more server specific such as security, custom error pages, etc. The <appSettings> tag holds more of the information that can be used on actual applications such as sql connection strings.

There are 2 different types of security - Authentication and Authorization. Authentication specifies the type of authentication used thorough the application such as Windows, Forms, Passport mode or None if no authentication is necessary. Authorization specifies which users can access which pages. An example of each type is listed below:

<system.web>
     <authentication mode="Forms" />
     <authorization>
          <allow roles="Administrators" />
          <deny users="*" />
     </authorization>
</system.web>

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

Another common setting to change in your Web.config file for your web site is custom error pages. Default server error pages are usually plain, basic, and aren't modeled after your template or contain possible suggestions for what page you were trying to access. By using the <customErrors> tag you can set each error to use a specific page.

An example is below:

<system.web>
     <customErrors mode="RemoteOnly" defaultRedirect="error.aspx">
          <error statusCode="404" redirect="pagemissing.aspx" />
     </customErrors>
</system.web>

Now that we've covered the most common child nodes of <system.web>, we can move on to <appSettings>.

Due to the extra security on Web.config files, important variable strings can be stored to avoid accidental exposure. It isn't uncommon for the debug variable to be set to true and errors allow unwanted eyes to see confidential information. A common example of this would be a database connection string. By using the <add> tags you can store name/value pairs to be accessed by your application.

This is an example of how to store a database connection string.

<appSettings>
     <add key="connString" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" />
</appSettings>

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

This is an example of the code you'd use to access the stored name/value pairs in the Web.config file.

//c#
Sqlconnection Sqlconn = New SqlConnection(ConfigurationManager.AppSettings["connString"]));

//vb.net
Dim SqlConn as New SqlConnection(ConfigurationManager.AppSettings["connString"]))

These are the basics on the Web.config file. More information can be found by visiting MSDN: Microsoft Developer Network.

Looking for more .NET Tutorials? Click Here!

123 ASP

411 ASP

Dot Net Freaks

Server Intellect