views:

2698

answers:

4

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?

A: 

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.

RedFilter
A: 

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

opensas
+3  A: 

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
AnthonyWJones
Looks good, I'll try it.Liked your comment on Jon's blog.
C. Ross
Not quite right unfortunately, see the edit for clarification.
C. Ross
+4  A: 

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("")
shovavnik
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.
Khalid Abuhakmeh
Though of course that approach doesn't work in in global.asa where you have no access to the Request object.
andynormancx