Get virtual path for a full path in asp classic
Asked Answered
C

6

8

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?

Catherin answered 20/5, 2009 at 14:11 Comment(2)
Can you clarify one more thing, where is this code running? In an ASP page in your app?Agile
Also what IIS version? IIS6?Agile
I
14

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("")
Intangible answered 3/6, 2010 at 20:13 Comment(3)
I really don't like classic asp due to these little quirks, which most modern web languages have addressed, but people still want Classic ASP sites... oh the humanity. But this worked like a charm, thank you.Ly
Though of course that approach doesn't work in in global.asa where you have no access to the Request object.Numismatics
This is one great solution to similar problem of mine!Toddler
A
4

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
Agile answered 20/5, 2009 at 20:10 Comment(1)
Not quite right unfortunately, see the edit for clarification.Catherin
T
0

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.

Triquetrous answered 20/5, 2009 at 14:20 Comment(0)
B
0

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...

Bakki answered 20/5, 2009 at 17:40 Comment(0)
S
0

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:

Scyphus answered 21/6, 2012 at 21:20 Comment(0)
S
0

The server's virtual path is:

<%Response.Write "http://" & Request.ServerVariables("server_name") &  
left(Request.ServerVariables("SCRIPT_NAME"),InStrRev(Request.ServerVariables("SCRIPT_NAME"),"/"))   %>
</p>
Swanherd answered 26/3, 2013 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.