Having classic ASP read in a key from appsettings in a web.config file

别说谁变了你拦得住时间么 提交于 2021-02-07 08:46:14

问题


OK so here's the situation. I've got a classic ASP website running inside an MVC 4 application. I need the classic ASP website to be able to get a key from the appsettings section of the web.config file.

Here is the function I've got:

' 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

Here is the function call code:

msn = ImportMySite("web.config", "mysite", false)

So when I call this function the value I get back is always blank or null. I'm not sure where I'm going wrong, I'm a total novice with XML so maybe I'm missing something completely obvious. I have searched the questions but couldn't find anything related to this using classic ASP.

Any help would be much appreciated.


回答1:


I appreciate Connor's work. It got me well along the way to making this happen. I made some changes that I think might be helpful to others.

I did not want to have to repeat the file name for each call and I have a couple of sections in my config. This seemed more general. Also, I consolidated his changes into a for-sure working example. You can paste this into your app, change the CONFIG_FILE_PATH and get on with your life.

'******************************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)



回答2:


OK I found my answer.

I changed:

For Each oAttr in oChild 
    If  oAttr.getAttribute("key") = attrName then
        dsn = oAttr.getAttribute("mysite")
        ImportMySite = dsn
        Exit Function
    End If
Next

To:

   For Each oAttr in oChild 
        If  oAttr.getAttribute("key") = attrName then
            dsn = oAttr.getAttribute("value")
            ImportMySite = dsn
            Exit Function
        End If
    Next


来源:https://stackoverflow.com/questions/28960446/having-classic-asp-read-in-a-key-from-appsettings-in-a-web-config-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!