When you create a web project in Visual Studio, a standard configuration file, named Web.config, is automatically created for you. This file is actually an XML document file. For a greater understanding regarding the format of this file, read the Microsoft reference titled Format of ASP.NET Configuration Files. If you want to store application-wide configuration information in an external file, such as an SQL Server connection string, it is usually recommended that you add an <appSettings> section to your Web.config file and store your information in this ‘additional’ section. This works well if the information is fairly static, and is not subject to change outside of your control.
One of my clients wanted to have some application-wide information available for their website, namely release information in text format, but did not wish to put it in the web.config file. They have a separate department that is responsible for doing builds and releasing them to QA and Production. The one thing this department wanted to do was to place a simple text file in the root web directory that any page could then have available. I suggested using a custom .config file, named Release.config. Below, I document what goes in this custom .config file and then how to access its values using a simple static method in a utility class.
Most samples of custom application configuration processing that you find on the web document how to add sections to an existing Web.config file. That particular solution was not appropriate here since the department responsible for producing the information was not a development department.
The steps are fairly simple:
- Create our own custom .config file which is itself an XML document.
- Modify the standard Web.config file to reference our custom .config file.
- Create a utility method to extract the desired values from the custom .config file.
- Modify our application code to utilize the utility method.