【问题标题】:Having classic ASP read in a key from appsettings in a web.config file让经典 ASP 从 web.config 文件中的 appsettings 中读取密钥
【发布时间】:2015-05-11 16:44:42
【问题描述】:

好的,情况就是这样。我有一个在 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 找不到与此相关的任何内容。

任何帮助将不胜感激。

【问题讨论】:

  • Set oXML=Server.CreateObject("Msxml2.DOMDocument.6.0") 将调用最新版本的 Microsoft XML 处理器。
  • 谢谢你,我已经更新了我的代码。

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


【解决方案1】:

我很欣赏康纳的工作。它让我顺利实现了这一目标。我做了一些我认为可能对其他人有帮助的改变。

我不想为每次调用都重复文件名,我的配置中有几个部分。这似乎更笼统。此外,我将他的更改合并为一个可靠的工作示例。您可以将其粘贴到您的应用中,更改 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)

【讨论】:

    【解决方案2】:

    好的,我找到了答案。

    我变了:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多