Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • 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?

like image 572
C. Ross Avatar asked May 20 '09 14:05

C. Ross


People also ask

Is a physical path but a virtual path was expected asp net c#?

Generally this physical and virtual path problem occurred whenever we refer “Server. MapPath” value multiple times while using folder path in applications. To solve this e:is a physical path but a virtual path was expected we need to use Server.

Where is virtual directory path in asp net?

Go to Websites & Domains and find the website's domain name. Click Virtual Directories. Browse to the required directory and click a link with its name. Click ASP.NET Settings.

Which of the following MVC methods converts a virtual path to a physical path?

MapPath method is used to map the virtual path to a physical path.

What is virtual path and physical path in asp net?

Physical path - This is the actual path the file is located by IIS. Virtual path - This is the logical path to access the file which is pointed to from outside of the IIS application folder.


2 Answers

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("")
like image 120
shovavnik Avatar answered Sep 22 '22 04:09

shovavnik


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
like image 42
AnthonyWJones Avatar answered Sep 21 '22 04:09

AnthonyWJones