获取asp classic中完整路径的虚拟路径

发布于 2024-07-21 07:00:01 字数 683 浏览 8 评论 0原文

如何获取 ASP classic 中完整路径的虚拟路径。 请注意,完整路径可能位于虚拟目录下,因此简单的

virtPath = Replace(fullPath, Server.MapPath("/"), "") 

方法将不起作用。

编辑:为了澄清,一个示例遵循

  • 完整的 Windows 文件路径(已知): \\MyServer\MyShare\Web\Site\Logs\Test.txt
  • 我的网站有一个虚拟目录 称为日志,指向\\MyServer\MyShare\Web\Site\Logs\
  • 虚拟路径(未知):/Logs/Text.txt
  • HTTP 路径(未知,需要): http://Site/Logs/Test.txt
  • 代码位于ASP 页面位于主应用程序中,不在任何虚拟目录下。 它位于与相关文件不同的服务器上。
  • IIS 6.0

    如何从完整文件路径中找到虚拟路径?

How can I get the virtual path for a full path in ASP classic. Note that the full path may be under a virtual directory and therefore the simplistic

virtPath = Replace(fullPath, Server.MapPath("/"), "") 

method will not work.

Edit: To clarify, an example follows

  • Full Windows File Path (known):
    \\MyServer\MyShare\Web\Site\Logs\Test.txt
  • My Website has a Virtual directory
    called Logs that Points to \\MyServer\MyShare\Web\Site\Logs\.
  • Virtual Path (unknown): /Logs/Text.txt
  • Http path (unknown, needed):
    http://Site/Logs/Test.txt
  • The code is in a asp page in the main app, not under any virtual directories. It is located on a separate server from the file in question.
  • IIS 6.0

    How do I find the virtual path from the full file path?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

只等公子 2024-07-28 07:00:02

如果有人感兴趣,安东尼·琼斯的回答向我展示了一致地获取应用程序相对根的方法。 因此,如果您有一个位于 http://example.com 的网站,并且在 http://localhost/example,你可以用这个函数找到你的根目录:

Function ToRootedVirtual(relativePath)
    Dim applicationMetaPath : applicationMetaPath = Request.ServerVariables("APPL_MD_PATH")
    Dim instanceMetaPath : instanceMetaPath = Request.ServerVariables("INSTANCE_META_PATH")
    Dim rootPath : rootPath = Mid(applicationMetaPath, Len(instanceMetaPath) + Len("/ROOT/"))
    ToRootedVirtual = rootPath + relativePath
End Function

然后你可以像这样调用它来获取根路径:

ToRootedVirtual("/")

它将返回:

  • / 在 example.com 上
  • /example/ 在 localhost/example 上

您也可以在不使用斜杠的情况下使用它:

ToRootedVirtual("")

In case anyone's interested, Anthony Jones' answer showed me the way to getting the application's relative root consistently. So if you have a site at http://example.com and a local development equivalent at http://localhost/example, you can find your root with this function:

Function ToRootedVirtual(relativePath)
    Dim applicationMetaPath : applicationMetaPath = Request.ServerVariables("APPL_MD_PATH")
    Dim instanceMetaPath : instanceMetaPath = Request.ServerVariables("INSTANCE_META_PATH")
    Dim rootPath : rootPath = Mid(applicationMetaPath, Len(instanceMetaPath) + Len("/ROOT/"))
    ToRootedVirtual = rootPath + relativePath
End Function

You can then call it like this to get the root path:

ToRootedVirtual("/")

Which will return:

  • / on example.com
  • /example/ on localhost/example

You can also use it without the slash:

ToRootedVirtual("")
诠释孤独 2024-07-28 07:00:02

如果我理解了这个问题。

假设

完整路径是当前应用程序或子应用程序中的路径。 它既不是仅限于父应用程序的路径,也不是进入同级应用程序的路径。 所需路径是相对于当前应用程序路径的。

场景 1

之类的路径

诸如“/someApp/someFolder/someSubFolder/file.ext”

应将其解析为:-

“~/someFolder/someSubFolder/file.ext”

(尽管 ~/ 符号不是这是 ASP 经典可以理解的东西)。

场景 2

“/someApp/someSubApp/SomeSubFolder/file.ext”

您仍然想要:-

“~/someFolder/someSubFolder/file.ext”

场景 3

该应用程序是网站的根应用程序:-

“/someFolder/someSubFolder/file.ext”

仍然会变成

“~/someFolder/someSubFolder.file.ext”

解决方案

解决此问题的关键是:-

Dim sAppMetaPath : sAppMetaPath = Request.ServerVariables("APPL_MD_PATH")

对于上述内容这将导致一组场景,例如:-

  1. “/LM/W3SVC/33230916/Root/someApp”
  2. “/LM/W3SVC/33230916/Root/someApp/someSubApp”
  3. “/LM/W3SVC/33230916/Root”

Dim sInstanceMetaPath: sInstanceMetaPath = Request.ServerVariables("INSTANCE_META_PATH")

将在所有场景都返回

“/LM/W3SVC/33230916”

通过一些数学简化,我们可以得出该函数:-

Function ToAppRelative(virtualPath)

    Dim sAppMetaPath : sAppMetaPath = Request.ServerVariables("APPL_MD_PATH")
    Dim sInstanceMetaPath: sInstanceMetaPath = Request.ServerVariables("INSTANCE_META_PATH")

    ToAppRelative = "~/" & Mid(virtualPath, Len(sAppMetaPath) - Len(sInstanceMetaPath) - 3)

End Function

If I've understood the question.

Assumption

The full path is a path with in the current application or a child application. It is not a path limited to the parent nor a path into a sibling application. The desired path is relative to the current applications path.

Scenario 1

A path such as

"/someApp/someFolder/someSubFolder/file.ext"

should resolve it to:-

"~/someFolder/someSubFolder/file.ext"

(although the ~/ notation isn't something ASP classic understands).

Scenario 2

"/someApp/someSubApp/SomeSubFolder/file.ext"

you still want:-

"~/someFolder/someSubFolder/file.ext"

Scenario 3

The app is the root application of the site:-

"/someFolder/someSubFolder/file.ext"

would still become

"~/someFolder/someSubFolder.file.ext"

Solution

The key to solving this is:-

Dim sAppMetaPath : sAppMetaPath = Request.ServerVariables("APPL_MD_PATH")

For the above set of scenarios this will result in something like:-

  1. "/LM/W3SVC/33230916/Root/someApp"
  2. "/LM/W3SVC/33230916/Root/someApp/someSubApp"
  3. "/LM/W3SVC/33230916/Root"

Also

Dim sInstanceMetaPath: sInstanceMetaPath = Request.ServerVariables("INSTANCE_META_PATH")

will in all the scenarios return

"/LM/W3SVC/33230916"

With some mathematical reduction we can arrive at the function:-

Function ToAppRelative(virtualPath)

    Dim sAppMetaPath : sAppMetaPath = Request.ServerVariables("APPL_MD_PATH")
    Dim sInstanceMetaPath: sInstanceMetaPath = Request.ServerVariables("INSTANCE_META_PATH")

    ToAppRelative = "~/" & Mid(virtualPath, Len(sAppMetaPath) - Len(sInstanceMetaPath) - 3)

End Function
暖伴 2024-07-28 07:00:02

尽管可能有更好的方法,但我总是通过创建一个配置变量来实现此目的,在该变量中手动指定不属于虚拟路径的根路径。 这是因为您不知道该站点是否会以 root 身份部署在根 Web 中的文件夹下,或者部署在虚拟目录中。

Although there may be a better way, I always did this by creating a config variable where I manually specify the root path that is not part of the virtual path. This is because you do not know if the site will be deployed as root, under a folder in root web, or in a virtual directory.

几度春秋 2024-07-28 07:00:02

好吧,我的答案并不比 OrbMan 的更好...

我已经以这样的方式组织了我的应用程序,每个包含都是相对的...

而不是

\myapp\lib\somefile.asp 我使用 ..\lib\somefile .asp

在其他情况下我只是按照 Orbman 所说的去做......

well, my answer isn't better than OrbMan's...

I have organized my app in such a way that every include is relative...

that is

instead of \myapp\lib\somefile.asp I use ..\lib\somefile.asp

in other cases I just do what Orbman said...

澜川若宁 2024-07-28 07:00:02

以下是如何通过 ASP 解决 html 中的根相关路径问题,以便您的站点可以移植到不同的托管目录。

这个小片段将生成正确的前缀来设置您的 URL:

Mid(Request.ServerVariables("APPL_MD_PATH"),Len(Request.ServerVariables("INSTANCE_META_PATH"))+6)

您可以在 LINK、IMG、超链接等中使用它,如下所示:

<link href="<%= Mid(Request.ServerVariables("APPL_MD_PATH"),Len(Request.ServerVariables("INSTANCE_META_PATH"))+6) %>/assets/css/master.css" rel="stylesheet" type="text/css" />

因此,将您的路径编码为相对于根目录(以 / 开头),然后将此片段放在正确的位置在第一个斜杠前面,引号内:

Here's how you solve root-relatie pathing in html via ASP so your site can be portable to different hosting directories.

This little snippet will produce the correct prefix to set your URLs:

Mid(Request.ServerVariables("APPL_MD_PATH"),Len(Request.ServerVariables("INSTANCE_META_PATH"))+6)

You can use this in LINKs, IMGs, hyperlinks, etc as follows:

<link href="<%= Mid(Request.ServerVariables("APPL_MD_PATH"),Len(Request.ServerVariables("INSTANCE_META_PATH"))+6) %>/assets/css/master.css" rel="stylesheet" type="text/css" />

so, code your paths to be root-relative (starts with a /) and then put this snippet right in front of that first slash, inside the quotes:

香草可樂 2024-07-28 07:00:02

服务器的虚拟路径是:

<%Response.Write "http://" & Request.ServerVariables("server_name") &  
left(Request.ServerVariables("SCRIPT_NAME"),InStrRev(Request.ServerVariables("SCRIPT_NAME"),"/"))   %>
</p>

The server's virtual path is:

<%Response.Write "http://" & Request.ServerVariables("server_name") &  
left(Request.ServerVariables("SCRIPT_NAME"),InStrRev(Request.ServerVariables("SCRIPT_NAME"),"/"))   %>
</p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文