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

时间:2015-03-10 09:48:12

标签: asp.net asp.net-mvc asp-classic web-config

好的,这就是情况。我在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

这是函数调用代码:

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

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

非常感谢任何帮助。

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

好的,我找到了答案。

我改变了:

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

要:

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