Web.config exposes an
Let us look at a basic web.config which holds our connection string.
xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="false" strict="false" explicit="true" />
system.web>
<appSettings>
<add key="myConnInfo" value="server=_;database=_;user=_;pass=_;" />
appSettings>
configuration>
To read the connection setting from the Config file, you have to use a single line of code:
System.Configuration.ConfigurationManager.AppSettings("ConnectionInfo")
Multiple Config Files
The appSettings element can contain a file attribute that points to an external file. I will change my web.config file to look like the following:
xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<compilation debug="false" strict="false" explicit="true" />
system.web>
<appSettings file="externalSettings.config"/>
configuration>
Next, we can create the external file "externalSettings.config" and add an appSettings section with our connection information and any other settings that we want to use.
If the external file is present, ASP.Net combines the appSettings values from web.config with those in the external file. If a key/value exists in both files, then ASP.Net will use the setting from the external file.
This feature is useful when one keeps user-specific or environment-specific settings in the external file. It is better to design web.config to contain those settings that are global, while each user setting is contained in an external file. This approach makes it easier to move around global web.config changes.
One caution to this approach is that ASP.Net runtime does not detect when the external file changes. Thus to launch a new version of the application with all changes in effect, one will need to make changes to the web.config itself.
And all things must come to an end
The main emphasis in this article was on Application Settings - Multiple Config Usage. I hope you found this article interesting and informative. I am open for suggestions and remarks, both negative and positive. Feel free to contact me at varungupta@jsrsolution.com
No comments:
Post a Comment