Get virtual path for full path in asp classic

How can I get the virtual path for the full path in ASP classic. Please note that the full path may be in the virtual directory and therefore simplified

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

the method will not work.

Edit: an example is provided for clarification:

  • Full path to the Windows file (known): \\ MyServer \ MyShare \ Web \ Site \ Logs \ Test.txt
  • My site has a virtual directory called Logs , which points to \\ MyServer \ MyShare \ Web \ Site \ Logs \ .
  • Virtual path (unknown): /Logs/Text.txt
  • Http path (unknown, necessary): http: //Site/Logs/Test.txt
  • The code is on the asp page in the main application, and not in any virtual directories. It is located on a separate server from the file in question.
  • IIS 6.0

    How to find virtual path from full file path?

+7
asp-classic server.mappath
source share
6 answers

In case anyone is interested, Anthony Jones answer showed me the way to ensure that the application is relative root consistently. So, if you have a website at http://example.com and the equivalent of local development at http: // localhost / example , you can find your root using 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 

Then you can call it so that you get the root path:

 ToRootedVirtual("/") 

What will return:

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

You can also use it without a slash:

 ToRootedVirtual("") 
+14
source share

If I understood the question.

Assumption

The full path is the path in the current application or child application. This is not a path limited by a parent, nor a path to a sister app. The desired path refers to the current application path.

Scenario 1

Path such as

"/someApp/SomeFolder/someSubFolder/file.ext"

must allow it: -

"~ / SomeFolder / someSubFolder / file.ext"

(although the designation ~ / is not what ASP classic understands).

Scenario 2

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

do you still want: -

"~ / SomeFolder / someSubFolder / file.ext"

Scenario 3

The application is the root application of the site: -

"/SomeFolder/someSubFolder/file.ext"

will become anyway

"~ / SomeFolder / someSubFolder.file.ext"

Decision

The key to solving this is: -

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

For the above scenario, this will lead to something like: -

  • "/ LM / W3SVC / 33230916 / Root / someApp"
  • "/ LM / W3SVC / 33230916 / Root / someApp / someSubApp"
  • "/ LM / W3SVC / 33230916 / Root"

Besides

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

will be returned in all scenarios

"/ LM / W3SVC / 33230916"

With some mathematical reduction, we can come to 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 
+4
source share

Although there may be a better way, I always did this by creating a configuration variable in which I manually specify a 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 the root network or in a virtual directory.

0
source share

well, my answer is no better than OrbMan's ...

I organized my application so that each of them related ...

i.e

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

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

0
source share

Here is how you decide the root link path in html via ASP so that your site can be portable to different hosting directories.

This small snippet will give the correct prefix for setting your urls:

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

You can use this in LINK, IMG, hyperlinks, etc. in the following way:

 <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, encode your paths as relative to the roots (starts with a /), and then place this snippet right in front of the first slash inside the quotes:

0
source share

Server virtual path:

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

All Articles