Introduction
Web.config acts as a central location for storing the information to be accessed by web pages. This information could be a Connection String stored at a centralized location so that it can be accessed in a data-driven page,Or any Key Value pair that will be needed by the page to use in your Code. If the connection string changes its just a matter of changing it at one place.
Background
While We can use Our Web Configs Key Value pair Directly in ASP.NET pages using the `ConfigurationManagers Class` , but can we use it our classical .asp pages?, Actually these class Supported in: 4.5, 4, 3.5, 3.0, 2.0 Only of ASP.NET But how we can do this in Active Server Pages (ASP)
Using the code
First thing first
AppSettings of Web.config
appSettings element helps us to store the application settings information like connection strings, file paths, URLs, port numbers, custom key value pairs, etc. The following code snippet shows the example of appSettings Section:
<appsettings>
<add key="AppKey" value="APLJI12345AFAFAF89999BDFG">
</add></appsettings><appsettings>
<add key="SuperUserValue" value="APLJI12345AFAFAF89999BDFG">
</add></appsettings>
For More On Web.config.. nice Article Here
http://www.codeproject.com/Articles/301726/Web-config-File-ASP-NET
All we need to load the entire web.config in our object and need to find the tag "appSettings" ,in there we need to loop to each "add" tag to find the correct tag which matches our need ie the key value pair which we need to point.
Dim xmlDocObj,xmlappSettingsObj,xmladdObj ,x
set xmlDocObj=server.CreateObject("Microsoft.XMLDOM")
set xmlappSettingsObj=server.CreateObject("Microsoft.XMLDOM")
set xmladdObj=server.CreateObject("Microsoft.XMLDOM")
xmlDocObj.async="false"
IF(xmlDocObj.load(server.MapPath ("/web.config"))) Then
set xmlappSettingsObj = xmldocObj.GetElementsByTagName("appSettings").Item(0)
set xmladdObj = xmlappSettingsObj.GetElementsByTagName("add")
for each x in xmladdObj
if x.getAttribute("key") ="SuperUserValue" then
MyVariable=x.getAttribute("value")
END IF
Points of Interest
The most annoying thing about the code chunk is
xmlDocObj.load(server.MapPath ("/web.config"))
you have to make sure the server maps to correct location ,or else you will fail to enter the If block, We can avoid the if block but if the loads fails the code will return object required error