如何找到IIS虚拟目录的完整虚拟路径?

huangapple go评论137阅读模式
英文:

How to find the full virtual path of an IIS VirtualDirectory?

问题

I'm trying to programmatically get a list of all the applications running on an IIS website, but when I query VirtualDirectory.Path I only ever get back "/", even though IIS shows the full path as expected.

I used the following script to reproduce my problem. I'm using pythonnet to interface with the C# API:

#! python3

import clr
import os
webAdmin = "system32\\inetsrv\\Microsoft.Web.Administration.dll"
clr.AddReference(os.path.join(os.environ["SystemRoot"], webAdmin))
from Microsoft.Web.Administration import ServerManager

def main():
    manager = ServerManager.OpenRemote("localhost")
    site = manager.Sites["Default Web Site"]
    poolName = "test-app-pool"
    manager.ApplicationPools.Add(poolName)
    newApp = site.Applications.Add("/another-test", "C:\\some\\path")
    newApp.ApplicationPoolName = poolName
    manager.CommitChanges()

    print(newApp.VirtualDirectories[0].Path)
    # Output: /

if __name__ == "__main__":
    main()

I expected the output to be "/another-test". Looking at the documentation it says VirtualDirectory.Path "Gets or sets the virtual path of the virtual directory relative to its parent". If I loop through all the applications and all the virtual directories on the site, each of them has a .Path of "/". How do I get the full virtual path?

英文:

I'm trying to programmatically get a list of all the applications running on an IIS website, but when I query VirtualDirectory.Path I only ever get back "/", even though IIS shows the full path as expected.

I used the following script to reproduce my problem. I'm using pythonnet to interface with the C# API:

#! python3

import clr
import os
webAdmin = "system32\\inetsrv\\Microsoft.Web.Administration.dll"
clr.AddReference(os.path.join(os.environ["SystemRoot"], webAdmin))
from Microsoft.Web.Administration import ServerManager

def main():
    manager = ServerManager.OpenRemote("localhost")
    site = manager.Sites["Default Web Site"]
    poolName = "test-app-pool"
    manager.ApplicationPools.Add(poolName)
    newApp = site.Applications.Add("/another-test", "C:\\some\\path")
    newApp.ApplicationPoolName = poolName
    manager.CommitChanges()

    print(newApp.VirtualDirectories[0].Path)
    # Output: /

if __name__ == "__main__":
    main()

I expected the output to be "/another-test". Looking at the documentation it says VirtualDirectory.Path "Gets or sets the virtual path of the virtual directory relative to its parent". If I loop through all the applications and all the virtual directories on the site, each of them has a .Path of "/". How do I get the full virtual path?

答案1

得分: 0

我相信你正在寻找的是Application类的Path属性,即newApp.Path包含你想要的内容。

英文:

I believe what you are looking for is Path property of the Application class, i.e. newApp.Path contains what you want.

huangapple
  • 本文由 发表于 2023年6月6日 01:13:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408659.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定