Server.MapPath returning wrong path

J

John Rebbeck

I've got the directory f:\Company\Product set as web shared so it's got a
virtual directory in the default web site on my test server's IIS. If I try
to use Server.MapPath in that site it returns a path within
c:\Inetpub\wwwroot rather than the real path.

Can anyone please tell me why this isn't working?

Regards,
John
 
A

Aaron Bertrand [MVP]

Server.MapPath uses the root of the default web site as its determination of
the local folder. It doesn't actually look at individual virtual
directories, for some bizarre reason... I think it's a bug.
 
J

John Rebbeck

Any way to get around it?

Aaron Bertrand said:
Server.MapPath uses the root of the default web site as its determination of
the local folder. It doesn't actually look at individual virtual
directories, for some bizarre reason... I think it's a bug.
 
P

Phillip Windell

I'm not sure but what does using the Server Variable
APPL_PHYSICAL_PATH produce?

I think the syntax would be:
Response.Write Request.ServerVariables("APPL_PHYSICAL_PATH ")
Be double check that to be sure.


--

Phillip Windell [CCNA, MVP, MCP]
(e-mail address removed)
WAND-TV (ABC Affiliate)
www.wandtv.com
 
M

Mark Schupp


I can't duplicate this behavior. MapPath always returns the correct path for
me. Maybe John could post his code and physical directory structure?

--
Mark Schupp
--
Head of Development
Integrity eLearning
Online Learning Solutions Provider
(e-mail address removed)
http://www.ielearning.com
714.637.9480 x17
 
P

Phillip Windell

Mark Schupp said:
I can't duplicate this behavior. MapPath always returns the correct path for
me. Maybe John could post his code and physical directory structure?

I've had it do that same to me. If you run a site from a Virtual
Folder instead of an actual Site the MapPath would use the parrent
Site as the starting point instead of the Virtual Directory root. This
is what you would run into with, say, Win2000 Pro where the IIS is
limited to one site, so each separate website runs from its own
Virtual Directory.

I got around it by just switching the Home Directory of the only Site
to what ever I needed at the moment....it is only a developement
machine so that works fine for me.

--

Phillip Windell [CCNA, MVP, MCP]
(e-mail address removed)
WAND-TV (ABC Affiliate)
www.wandtv.com
 
M

Mark Schupp

....
I've had it do that same to me. If you run a site from a Virtual
Folder instead of an actual Site the MapPath would use the parrent
Site as the starting point instead of the Virtual Directory root. This
is what you would run into with, say, Win2000 Pro where the IIS is
limited to one site, so each separate website runs from its own
Virtual Directory.

Still not clear on what you mean by each separate "website" running in its
own virtual directory when you can only have a single web site on the
system. Do you mean that there is a way to make it look like there are
multiple sites on the system? What would the URLs to 2 separate "websites"
look like?

--
Mark Schupp
--
Head of Development
Integrity eLearning
Online Learning Solutions Provider
(e-mail address removed)
http://www.ielearning.com
714.637.9480 x17
 
T

Tom B

Windows 2000 server (and I assume 2003) allow more than one site on a
computer, each usually has it's own public IP

Windows 2000 and XP professional only allow a single site (Default Web Site)
 
M

Mark Schupp

Tom B said:
Windows 2000 server (and I assume 2003) allow more than one site on a
computer, each usually has it's own public IP

Windows 2000 and XP professional only allow a single site (Default Web Site)
Yes, I know. What I am curious about is the configuration that is causing
the problem. I cannot duplicate it on my systems (2kpro or server).

I was trying to find out what the last poster meant by "If you run a site
from a Virtual Folder instead of an actual Site".

--
Mark Schupp
--
Head of Development
Integrity eLearning
Online Learning Solutions Provider
(e-mail address removed)
http://www.ielearning.com
714.637.9480 x17
 
A

Aaron Bertrand [MVP]

Still not clear on what you mean by each separate "website" running in its
own virtual directory when you can only have a single web site on the
system. Do you mean that there is a way to make it look like there are
multiple sites on the system? What would the URLs to 2 separate "websites"
look like?

No, do this.

Under the default web site, create a virtual directory called 'foo' which
points to somewhere *other* than your normal webroot (e.g. if you use
c:\inetpub\wwwroot\ for your webroot, create a folder called c:\whatever\
and use that folder).

Now, fuss with the permissions so you can run ASP from that folder, and
place an ASP file like this test.asp:

<%
response.write server.mappath("/foo/test.asp")
%>

When you run it, you'll see that it returns something like:

c:\inetpub\wwwroot\foo\test.asp

Instead of what I think it should return:

c:\whatever\test.asp
 
M

Mark Schupp

Aaron,

Still cannot duplicate it. I tried the following on my system against some
existing virtual dirs:

<%
Response.Write "Server.MapPath(""/global.asa""): " &
server.MapPath("/global.asa") & "<br>"
Response.Write "Server.MapPath(""kx_gowbt.asp""): " &
server.MapPath("kx_gowbt.asp") & "<br>"
Response.Write "Server.MapPath(""/kx/kx_gowbt.asp""): " &
server.MapPath("/kx/kx_gowbt.asp")
%>

Returns:

Server.MapPath("/global.asa"): C:\Inetpub\wwwroot\global.asa
Server.MapPath("kx_gowbt.asp"): C:\bw\careertracker\kx_gowbt.asp
Server.MapPath("/kx/kx_gowbt.asp"): C:\bw\careertracker\kx_gowbt.asp

Which, unless I am going completely ga-ga, is correct. Do you think there
could be a problem with a specific version of IIS? I am running Win2k/IIS5

--
Mark Schupp
--
Head of Development
Integrity eLearning
Online Learning Solutions Provider
(e-mail address removed)
http://www.ielearning.com
714.637.9480 x17
 
P

Phillip Windell

Mark Schupp said:
Still cannot duplicate it. I tried the following on my system against some
existing virtual dirs:

To make sure I'm sane ;) I tested it. Here is what I did.

Using Win2000 Pro (IIS only allows one Site, but multiple VFs)

Home path for the Default Website is "C:\Inetpub\wwwroot"

Created a Virtual Folder called "Test"
Home path for Virtual Folder is "D:\Temp"

Created an ASP file called Test.asp and placed it in "D:\Temp"

File contains the line:
<%Response.write Server.MapPath("/")%> & "<br>"

The output from the page is:
"C:\Inetpub\wwwroot"

I then added the line:
<%Response.Write Request.ServerVariables("APPL_PHYSICAL_PATH")%>

This produced:
"D:\Temp\"

So the final outcome is that using Server.MapPath will map to the Root
of the *Site*, while using
Request.ServerVariables("APPL_PHYSICAL_PATH") will map to the Root of
the *Virtual Folder*. So if the original poster of this thread uses
this instead of Server.MapPath his problem would be solved. Now I
assume that if either command is used from the Root of the Site they
will both produce the same results.

--

Phillip Windell [CCNA, MVP, MCP]
(e-mail address removed)
WAND-TV (ABC Affiliate)
www.wandtv.com
 
M

Mark Schupp

Maybe I'm naive but when you use "/" aren't you asking for the root of the
site?

If you want the root of the virtual directory then you would need
"/<virtdir>/".

--
Mark Schupp
--
Head of Development
Integrity eLearning
Online Learning Solutions Provider
(e-mail address removed)
http://www.ielearning.com
714.637.9480 x17


Phillip Windell said:
Mark Schupp said:
Still cannot duplicate it. I tried the following on my system against some
existing virtual dirs:

To make sure I'm sane ;) I tested it. Here is what I did.

Using Win2000 Pro (IIS only allows one Site, but multiple VFs)

Home path for the Default Website is "C:\Inetpub\wwwroot"

Created a Virtual Folder called "Test"
Home path for Virtual Folder is "D:\Temp"

Created an ASP file called Test.asp and placed it in "D:\Temp"

File contains the line:
<%Response.write Server.MapPath("/")%> & "<br>"

The output from the page is:
"C:\Inetpub\wwwroot"

I then added the line:
<%Response.Write Request.ServerVariables("APPL_PHYSICAL_PATH")%>

This produced:
"D:\Temp\"

So the final outcome is that using Server.MapPath will map to the Root
of the *Site*, while using
Request.ServerVariables("APPL_PHYSICAL_PATH") will map to the Root of
the *Virtual Folder*. So if the original poster of this thread uses
this instead of Server.MapPath his problem would be solved. Now I
assume that if either command is used from the Root of the Site they
will both produce the same results.

--

Phillip Windell [CCNA, MVP, MCP]
(e-mail address removed)
WAND-TV (ABC Affiliate)
www.wandtv.com
 
P

Phillip Windell

Ok. Using Server.MapPath("\test\") produces the Root of the Virtual
folder which was "D:\Temp". I noticed that this leaves off the
trailing \ while APPL_PHYSICAL_PATH includes it.

So maybe the original poster was using:
Server.MapPath("\") & "\test\something.asp"
.... instead of:
Server.MapPath("\test\something.asp")

I just tried that and it does reproduce his problem.

I don't know, he never showed any code.


--

Phillip Windell [CCNA, MVP, MCP]
(e-mail address removed)
WAND-TV (ABC Affiliate)
www.wandtv.com

Mark Schupp said:
Maybe I'm naive but when you use "/" aren't you asking for the root of the
site?

If you want the root of the virtual directory then you would need
"/<virtdir>/".

--
Mark Schupp
--
Head of Development
Integrity eLearning
Online Learning Solutions Provider
(e-mail address removed)
http://www.ielearning.com
714.637.9480 x17


Phillip Windell said:
Mark Schupp said:
Still cannot duplicate it. I tried the following on my system against some
existing virtual dirs:

To make sure I'm sane ;) I tested it. Here is what I did.

Using Win2000 Pro (IIS only allows one Site, but multiple VFs)

Home path for the Default Website is "C:\Inetpub\wwwroot"

Created a Virtual Folder called "Test"
Home path for Virtual Folder is "D:\Temp"

Created an ASP file called Test.asp and placed it in "D:\Temp"

File contains the line:
<%Response.write Server.MapPath("/")%> & "<br>"

The output from the page is:
"C:\Inetpub\wwwroot"

I then added the line:
<%Response.Write Request.ServerVariables("APPL_PHYSICAL_PATH")%>

This produced:
"D:\Temp\"

So the final outcome is that using Server.MapPath will map to the Root
of the *Site*, while using
Request.ServerVariables("APPL_PHYSICAL_PATH") will map to the Root of
the *Virtual Folder*. So if the original poster of this thread uses
this instead of Server.MapPath his problem would be solved. Now I
assume that if either command is used from the Root of the Site they
will both produce the same results.

--

Phillip Windell [CCNA, MVP, MCP]
(e-mail address removed)
WAND-TV (ABC Affiliate)
www.wandtv.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top