Server.Mappath and Virtual Directories

G

Guest

Hello,

I have a web application with a virtual directory in it. With 'virtual
directory' i mean
a folder whose physical path is different from the physical path of the
application, but in which Web Sharing is active.
For example:
my application is in "C:\Inetpub\wwwroot\MyApp",
the Virtual folder is in "C:\VirtualFolder",
and IIS sees it as if it were in "C:\Inetpub\wwwroot\MyApp\VirtualFolder".

How can I get, from code, the real physical path of the virtual folder?
I tried with Server.Mappath, but I found that it returns
"C:\Inetpub\wwwroot\MyApp\VirtualFolder", not "C:\VirtualFolder"

Thanks
Lorenzo
 
J

Juan T. Llibre

Good catch, Lorenzo.

The behavior is as you describe it, and I agree that Server.MapPath should
reflect the true physical directory of a virtual directory within an Application.

Please go to the Product Feedback Center :
http://lab.msdn.microsoft.com/productfeedback/default.aspx
and file a bug for that behavior.

You will need a Passport account to file a bug at the Feedback Center.

Thank you!
 
G

Guest

Thank you for the answer: I will surely report the 'Bug'.

In the meantime, could you (or someone other) suggest me another way to do
the same thing, or a workaround?

Thanks
 
J

Juan T. Llibre

re:
Thank you for the answer: I will surely report the 'Bug'.

Please do so.

That's the only way to let the Dev Team know what we know doesn't work,
so they can try to fix it...and that's the best way to get a better product.

re:
In the meantime, could you (or someone other) suggest me another way to do
the same thing, or a workaround?

The only workaround I can think of is not to use Server.MapPath
and, instead, hard-code the physical path.
 
J

Juan T. Llibre

Actually, I did quite a bit of testing with IIS after I read your post.
It happens with IIS and I can repro it.

You are mistaken about it only happening with Cassini.
File the bug or, if you are not up to it, I will.
 
G

Guest

Maybe I did a test sligthly different?

I Wasn't in debug mode.

In the first test, I tried opening IE and writing directly a path like this:
http:\\localhost\Projects\2006\myapp\WebService.asmx
(I use a WebService for the test: when invoked, it returns the path of a
virtual directory, generated via Server.Mappath).
This test works correctly!

In the second test, I tried opening IE and writing directly a path like this:
http:\\localhost:1263\myapp\WebService.asmx
(in this way I use Cassini, it's right?).
In this test the problem happens!

I made some error?
 
J

Juan T. Llibre

You never mentioned that you were talking about a Web Service.

I tested on an aspx page, which is the *usual* type of "web application",
which is all you mentioned in your original post, so, yes, it does make for
"slightly different" testing conditions.

Post webservice.asmx so we can dissect the code in it.
It dosn't have earth-shaking super-secret code in it, does it ?
 
G

Guest

Today I'm out of office, but tomorrow, I will try with a web page (I thought
it was the same thing than a webservice), and I'll post to you my code.

See you tomorrow!
 
G

Guest

I tested the same thing with a webpage:
the problem was identical, as I supposed.

The code to reproduce the test is quite simple:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs)Handles Me.Load
Label2.Text = Server.MapPath("Prova")
End Sub

(where Label2 is a Label Webcontrol and "Prova" is the virtual directory)


The result is correct if, in IE, I reach the page using this address:
http:\\localhost\Projects\2006\myapp\default2.aspx
(so using IIS)

The result is wrong if, in IE, I reach the page using this address:
http:\\localhost:1263\myapp\default2.aspx
(so using Cassini)

With the webservice, the code was:

<WebMethod()> _
Public Function AbsoluteEventsPath() As String
Return Server.MapPath("Prova")
End Function


In my opinion, this is exactly the situation described in the filed bug, and
it's due to a 'limitation' of Cassini that does not behave like IIS.
(Luckily, this is not a big deal for me, because in the production server I
use IIS).

If you went deeper than me and found a different situation, file it by
yourself, and please notify it to me, that I am very curoius!

(my e-mail is lorenzoaaa@REMOVE_MEtiscali.it)

Bye
Lorenzo
 
J

Juan T. Llibre

Try, if "Prova" is a virtual directory/application :

Server.MapPath("/Prova")

Notice the slash...
 
G

Guest

This isn't a correct test to try: if I write "/Prova", it searches a folder
directly in my localhost root, not in the current web application!
You can try by yourself. Notice the different behavior of Cassini and IIS:
IIS returns
"C:\Inetpub\wwwroot\Prova", Cassini raises an error!

By
 
Joined
Jan 28, 2009
Messages
1
Reaction score
0
Workaround

I have had the same problem.

(nice: this forum says "To be able to post links or images your post count must be 50 or greater. You currently have 0 posts." I AM NOT POSTING ANY LINKS, I JUST REGISTERED TO OFFER A SOLUTION YOU *@#*@#)

http;//site----> c:\wwwroot
http;//site/path ----> d:\sites\pathroot
http;//site/path/uploads ----> d:\sites\upl
http;//site/path/scripts/s.asp -----> d:\sites\pathroot\scripts\s.asp

from s.asp:
Server.MapPath(".") correctly returned d:\sites\pathroot\scripts\
Server.MapPath("../upl") incorrectly returned d:\sites\pathroot\upl

however
Server.Mappath("/path/scripts/../upl") correctly returns d:\sites\upl

SO THE UNIVERSAL WORKAROUND IS:

Code:
Function MapPath(Url) ' use this instead of Server.MapPath
  Url = Replace("\","/") ' just to be torough
  If Left(Url,1) = "/" Then
    MapPath = Server.MapPath(Url)
  Else
    Dim ScriptPath
    ScriptPath = Replace(Reques.ServerVariables("SCRIPT_NAME"),"\","/")
    ScriptPath = Left(ScriptPath, InStrRev(ScriptPath, "/"))
    ' Thank you, Bart Kemps!
    ' (Be a dear and leave this comment)
    MapPath = Server.MapPath(ScriptPath & Url)
  End If
End Function
 

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,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top