在web.config文件中使用appsettings中的密钥读取经典ASP

Con*_*ill 3 asp.net asp.net-mvc web-config asp-classic

好的,这就是情况.我在MVC 4应用程序中运行了一个经典的ASP网站.我需要经典的ASP网站才能从web.config文件的appsettings部分获取密钥.

这是我得到的功能:

' Imports a site string from an xml file (usually web.config)
Function ImportMySite(webConfig, attrName, reformatMSN)
    Dim oXML, oNode, oChild, oAttr, dsn
    Set oXML=Server.CreateObject("Microsoft.XMLDOM")
    oXML.Async = "false"
    oXML.Load(Server.MapPath(webConfig))
    Set oNode = oXML.GetElementsByTagName("appSettings").Item(0) 
    Set oChild = oNode.GetElementsByTagName("add")
    ' Get the first match
    For Each oAttr in oChild 
        If  oAttr.getAttribute("key") = attrName then
            dsn = oAttr.getAttribute("mysite")
            ImportMySite = dsn
            Exit Function
        End If
    Next
End Function
Run Code Online (Sandbox Code Playgroud)

这是函数调用代码:

msn = ImportMySite("web.config", "mysite", false)
Run Code Online (Sandbox Code Playgroud)

所以当我调用这个函数时,我得到的值总是空白或为空.我不确定我哪里出错了,我是XML的新手,所以也许我错过了一些完全明显的东西.我搜索过这些问题但是找不到任何与此相关的东西使用经典的ASP.

任何帮助将非常感激.

tqw*_*ite 5

我很欣赏康纳的工作.它让我顺利完成了这一切.我做了一些改变,我认为可能对其他人有所帮助.

我不想为每个调用重复文件名,我的配置中有几个部分.这似乎更普遍.此外,我将他的更改合并到一个肯定的工作示例中.您可以将其粘贴到您的应用中,更改CONFIG_FILE_PATH并继续您的生活.

'******************************GetConfigValue*******************************
' Purpose:      Utility function to get value from a configuration file.
' Conditions:   CONFIG_FILE_PATH must be refer to a valid XML file
' Input:        sectionName - a section in the file, eg, appSettings
'               attrName - refers to the "key" attribute of an entry
' Output:       A string containing the value of the appropriate entry
'***************************************************************************

CONFIG_FILE_PATH="Web.config" 'if no qualifier, refers to this directory. can point elsewhere.
Function GetConfigValue(sectionName, attrName)
    Dim oXML, oNode, oChild, oAttr, dsn
    Set oXML=Server.CreateObject("Microsoft.XMLDOM")
    oXML.Async = "false"
    oXML.Load(Server.MapPath(CONFIG_FILE_PATH))
    Set oNode = oXML.GetElementsByTagName(sectionName).Item(0) 
    Set oChild = oNode.GetElementsByTagName("add")
    ' Get the first match
    For Each oAttr in oChild 
        If  oAttr.getAttribute("key") = attrName then
            dsn = oAttr.getAttribute("value")
            GetConfigValue = dsn
            Exit Function
        End If
    Next
End Function



settingValue = GetConfigValue("appSettings", "someKeyName")
Response.Write(settingValue)
Run Code Online (Sandbox Code Playgroud)