C. Ross
C. Ross

Reputation: 31878

Get virtual path for a full path in asp classic

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

Upvotes: 8

Views: 24367

Answers (6)

Bipin Gharat
Bipin Gharat

Reputation: 1

The server's virtual path is:

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

Upvotes: 0

KenF
KenF

Reputation: 132

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:

Upvotes: 0

shovavnik
shovavnik

Reputation: 2937

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("")

Upvotes: 14

AnthonyWJones
AnthonyWJones

Reputation: 189555

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

Upvotes: 4

opensas
opensas

Reputation: 63625

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

Upvotes: 0

D&#39;Arcy Rittich
D&#39;Arcy Rittich

Reputation: 171579

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.

Upvotes: 0

Related Questions