【问题标题】:Set up a variable in web.config or global.asa to point to a server environment variable在 web.config 或 global.asa 中设置变量指向服务器环境变量
【发布时间】:2015-01-16 15:14:15
【问题描述】:

我是系统管理员,我希望只有一个地方可以更改/检查所有 web.config 或 global.asa 文件中的 NAS 路径,这样,每当路径更改时,我都会这样做不必对所有 web.config 或 global.asa 文件进行必要的更改,并希望避免任何错误或忘记文件...

我的想法是在本地服务器上有一个可以设置和更新的环境变量。

<!-- metadata type="typelib" file="C:\Program Files\Common Files\System\ADO\msado15.dll" -->
<script language="vbscript" runat="server">

Sub Application_OnStart

dim strNAS
    strNAS = ${MyEnvironmentVar} ???
Application("NAS") =  strNAS

End Sub


' Instead of using an hard-coded path like below
'Application("News")="File Name=\\NAS04.MyDomain.com\mydata$\myfile.udl"
'I'd like to use a more flexible way...

Application("News")="File Name=" + Application("NAS") + "mydata$\myfile.udl"
or 
Application("News")="File Name=" + ${MyEnvironmentVar} + "mydata$\myfile.udl"

...

</script>

$MyEnvironmentVar是本地服务器上设置为环境变量的变量,具有当前存储系统的路径...

MyEnvironmentVar = "\\NAS04.myDomain.com\" 

我在网上看到类似Environment.getenvironmentvariable() 的东西,但我不知道如何使用它以及它是否适用于 web.config 和 global.asa 文件。

【问题讨论】:

    标签: vbscript web-config environment-variables global.asa


    【解决方案1】:

    在 Global.asa 文件中,您可以尝试以下代码:

    Set objWSH = CreateObject("WScript.Shell")
    Set objUserVariables = objWSH.Environment("USER") 
    path = objUserVariables("NAS") ' here NAS is name environment variable stored in User variables
    

    如果 NAS 存储在系统变量中,则相应地更改第二行。

    如果你想在这里使用 web.config 方法。然而,在经典 ASP 中,web.config 只是另一个 XML 文件。 您的 web.config 如下所示:

    <configuration>
      <appSettings>
         <add key="NAS" value="your path goes here" />
      </appSettings>
    </configuration>
    

    VBScript 代码如下:

    set xmlDoc = server.CreateObject("Microsoft.XMLDOM")
    set xmlappSettings = server.CreateObject("Microsoft.XMLDOM")
    set xmladd = server.CreateObject("Microsoft.XMLDOM")
    xmlDoc.async = false
    xmlDoc.load(server.MapPath("web.config"))
    set xmlappSettings = xmldoc.GetElementsByTagName("appSettings").Item(0) 
    set xmladd = xmlappSettings.GetElementsByTagName("add")
    for each addItem in xmladd 
      if addItem.getAttribute("key") = "NAS" then
        myVar = addItem.getAttribute("value")
      end if
    next
    

    【讨论】:

    • 是的,没有。这就是托管方法Environment.GetEnvironmentVariable() 的用途。编辑:没关系,Global.asa 不是用于 ASP.NET,而是用于经典 ASP。
    • 但我相信有问题的应用程序是经典的 asp 应用程序。对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多