Server.MapPath returns unexpected. Can anyone help
I have setup my web with nested virtual directories as follows:<BR>/--------<BR>|------- reports<BR>......|-------- system1<BR>......|-------- system2<BR>where both reports, system1, system2 are virtual and system1 and system2 are under reports.<BR><BR>When I tried to use Server.Mappath(../reports/system1) in some<BR>directories(other than reports and it's sub.) under my IIS root,<BR>it returned only the system path for reports with a non-exsitence<BR>directory system1.<BR><BR>However, if I tried Server.Mappath(/reports/system1)), it<BR>return the correct system path for me.<BR><BR>Can anyone tell it is the expected behaviour for<BR>Server.MapPath ?<BR><BR>Thanks a lot.
.. means ''up one level''
So let's say you have:<BR><BR>wwwroot<BR> reports<BR> system1<BR> system2<BR> fruit<BR> apples<BR> fuji<BR> granny_smith<BR> bananas<BR> <BR>If you are in the "reports" directory and use "..", you get to "wwwroot" (the normal root directory). <BR><BR>So "../reports/system1" gets you where you expect to be.<BR><BR>If you are in the "fruits" directory and use "..", you get to "wwwroot" (the normal root directory). Same as for "reports" in other words.<BR><BR>If you are *already* in "reports/system1", and you then use "..", you get to "wwwroot
eports". So, then "../reports/system1" will *try* to get you to "wwwroot
eportssystem1". <BR><BR>If you are *already* in "fruits/apples/fuji", and you then use "..", you get to "wwwrootfruitsapples". So, then "../reports/system1" will *try* to get you to "wwwrootfruitsapples
eportssystem1". <BR><BR>This is NO DIFFERENT than if you use .. at the start of a path name from the DOS command line (e.g., "CDIR ..
eportssystem1" does the same kind of relative movement).<BR><BR>The whole reason you are allowed to start a path with / is to ensure that you are back up at the top directory of the virtual root.<BR><BR>So...I dunno why the results are unexpected to you. They seem right to me.<BR><BR><BR><BR>If you are in the "fruits" directory
RE: .. means ''up one level''
Thanks for you discussion, Let me explain more why I think the result is unexpected.<BR><BR>In my last example:<BR><BR>reports is a virtual directory and mapped to physical path<BR>at ,say, d:workdata.<BR><BR>Also the system1 is a virtual too and mapped to physical path<BR>at d:system1
eports<BR><BR>What I expected the mappath would returns to me if I <BR>issue Server.MapPath("../reports/system1") is the physical<BR>path, i.e. d:system1
eports<BR><BR>It seems that there are two different behaviours of MapPath<BR>that when I use absolute URL from the root (
eportsystem1),<BR>it returns d:system1
eports. However when I use relative path<BR>(..
eportsystem1), it returns d:workdatasystem1<BR><BR>That is what I unexpected and want to clarify the MapPath<BR>behaviour.<BR><BR>Using absolute URL seems to be fine, however, it is not easy<BR>run the code in different setting. It is because, I will run<BR>the code in my client server from the root URL and I need to<BR>setup a demo in my notebook's PWS which do not allow me to setup<BR>more than 1 host. I need to run it from a vitural directory.<BR>Thus if I put the code in an virtaul directory, I need to change<BR>the code MapPath("/reports/system1") to MapPath<BR>"/SomeInstalledVirtualDirectory/reports/system1").<BR><BR><BR>
...it *completely* depends on where you *start* from.<BR><BR>Note that Server.MapPath *DOES NOT* check to see if the path it generates is a legal one! It is *NOT* an error to give a non-existent path to Server.MapPath; it is not an error to get back a non-existent path.<BR><BR>> However when I use relative path <BR>> (..
eportsystem1), it returns d:workdatasystem1 <BR><BR>Now *THAT* I find hard to believe!<BR><BR>I would have expected it to return something like<BR> d:workdata
eportsystem1<BR><BR>I don't know of any way that Server.MapPath can "lose" a directory that you give it, when you use a relative path.<BR><BR>But never mind. Even if you stated it correctly, I say again: Using a relative path with Server.MapPath, you get back a relative result. Period.<BR><BR>Why are you concerned? If you want to ensure you are starting in the right virtual dir, just start with "/" and it should all work.<BR><BR><BR>