Sunday, February 6, 2011

How do I troubleshoot my web.config file?

The web.config file is a configuration file for the ASP.NET web applications. An ASP.NET application has one web.config file which keeps the configurations required for the corresponding application. The web.config file is written in XML with specific tags having specific meanings. There are  a number of important settings that can be stored in the web.config configuration file.

Here are some of the most frequently used configurations, stored conveniently inside web.config file:

  • Database connections;
  • Session States;
  • Error Handling;
  • Security.
Below we discuss database connections and error handling as examples:

Database Connections

The most important configuration data that can be stored inside the web.config file is the database connection string. Storing the connection string in the web.config file makes sense, since any modifications to the database configurations can be maintained at a single location. Otherwise, we’ll have to keep it either as a class level variable in all the associated source files or probably keep it in another class as a public static variable.
If the database information is stored in the web.config file, it can be read and used anywhere in the program. This will save alteration in different files where old connections are referenced.
Below is an example of a connection string stored in the web.config file:
1<configuration>
2<appSettings>
3<add key="ConnectionString" value="<a href="http://www.codersource.net/asp_net_web_configuration_file.html" target="_top">server</a>=mysqlhostsqlexpress;uid=USERNAME;pwd=PASSWORD;database=DatabaseID" /></appSettings></configuration>
Note: The DATABASEID, USERNAME, PASSWORD are found in your Netfirms Control Panel in the MSSQL section.

Error Handling:

Error handling is one of the most important part of any web application. Each error has to be caught and suitable action has to be taken to resolve that problem. ASP.NET web.config file lets us configure, what to do when an error occurs in our application.
Check the following xml tag in the web.config file that deals with errors:
1<customErrors mode = "On">
2<error statusCode = "404" redirect = "errorPage.aspx" />
3</customErrors>
This tells the ASP.NET to display custom errors from a remote client or a local client and to display a page named errorPage.aspx. Error “404″ is “Page not found” error.
Note: We suggest to turn customErrors mode to “off” (eg. <customErrors mode = “Off”>) so you will see ASP.NET default error messages. These error messages are useful for debugging purposes but should never be exposed to your visitors. Your visitors should always be presented with friendly errors if any.
For additional ASP.NET support and resources we recommend the following web sites: