从web.config文件中的appsettings中读取经典ASP的密钥 [英] Having classic ASP read in a key from appsettings in a web.config file

查看:80
本文介绍了从web.config文件中的appsettings中读取经典ASP的密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这是我所拥有的功能:

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

因此,当我调用此函数时,返回的值始终为空白或null.我不确定哪里出问题了,我是XML的新手,所以也许我错过了一些显而易见的东西.我已经搜索了问题,但使用经典的ASP找不到与此相关的任何内容.

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.

任何帮助将不胜感激.

推荐答案

我感谢Connor的工作.在实现这一目标的过程中,一切进展顺利.我进行了一些更改,我认为这些更改可能会对其他人有所帮助.

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.

我不想为每个调用重复文件名,并且我的配置中有几个部分.这似乎更笼统.另外,我将他的更改合并为一个肯定的工作示例.您可以将其粘贴到您的应用中,更改CONFIG_FILE_PATH并开始自己的生活.

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)

这篇关于从web.config文件中的appsettings中读取经典ASP的密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆