Server.MapPath("~") Ends with a \ on the server but not locally

  • Thread starter Thread starter AAaron123
  • Start date Start date
A

AAaron123

I noticed that the returned string from

Server.MapPath("~")

Ends with a \ on the server but not locally.

Could anyone comment on that?



Thanks
 
AAaron123 said:
I noticed that the returned string from

Server.MapPath("~")

Ends with a \ on the server but not locally.

Could anyone comment on that?



Thanks

However,
Server.MapPath("~/")

and

Server.MapPath("~/Test")

returns the same (and the expected) path on both sites so I guess the above
does not mater.
 
AAaron123 used his keyboard to write :
I noticed that the returned string from

Server.MapPath("~")

Ends with a \ on the server but not locally.

Could anyone comment on that?



Thanks

If you have a "root site", (the url is www.company.com), then
Server.MapPath("~") will return "/" (=only a /).

On your development system you usually have localhost/path-to-project
as your site-root. Then the method will return "/path-to-project" (=
path without trailing /)

Hans Kesting
 
Hans said:
AAaron123 used his keyboard to write :

If you have a "root site", (the url is www.company.com), then
Server.MapPath("~") will return "/" (=only a /).

On your development system you usually have localhost/path-to-project
as your site-root. Then the method will return "/path-to-project" (=
path without trailing /)

Hans Kesting

I'm having a problem understanding the difference between application-root
and site-root.

Thanks
 
AAaron123 said:
I noticed that the returned string from

Server.MapPath("~")

Ends with a \ on the server but not locally.

Could anyone comment on that?

Is there anything else different between the server and your dev PC? E.g.
OS, whether they're mapped drives, ASP.NET version?

FWIW, I get no trailing \ on IIS6.0 with ASP.NET 3.5, all up to date, local
drives. I do get a trailing \ if I use Server.MapPath("~\") or
Server.MapPath("~/").

As long as you use "Path.Combine" rather than "&" to concatenate paths,
there should not be any problem.

Andrew
 
Andrew said:
Is there anything else different between the server and your dev PC?
E.g. OS, whether they're mapped drives, ASP.NET version?

FWIW, I get no trailing \ on IIS6.0 with ASP.NET 3.5, all up to date,
local drives. I do get a trailing \ if I use Server.MapPath("~\") or
Server.MapPath("~/").

I don't get one when I run on VS2008.

Only after I copy the site to the ISP and connect there do I see it.

For your above comments were you rnning on a ISP or in a development system?

As long as you use "Path.Combine" rather than "&" to concatenate
paths, there should not be any problem.

Oh-oh. Had not heard about that. Use & many times. What is the problem
there??


Thanks a lot
 
AAaron123 said:
I don't get one when I run on VS2008.

Only after I copy the site to the ISP and connect there do I see it.

For your above comments were you rnning on a ISP or in a development
system?

I use a 32-bit Windows Server 2003 PC as a dev machine. I don't do testing
on our ISP-based machines.

Incidentally, I also tried it on 32-bit Vista (as in IIS7.0) with the same
results.
Oh-oh. Had not heard about that. Use & many times. What is the problem
there??

IO.Path.Combine takes care of a trailing path delimiter, or the absence of
one, for you.

Path.Combine("C:\website", "data") gives C:\website\data
Path.Combine("C:\website\", "data") also gives C:\website\data

Incidentally, if you use HttpRuntime.AppDomainAppPath instead of
Server.MapPath("~"), do you consistently get the trailing backslash?

Andrew
 
Andrew said:
I use a 32-bit Windows Server 2003 PC as a dev machine. I don't do
testing on our ISP-based machines.

Incidentally, I also tried it on 32-bit Vista (as in IIS7.0) with the
same results.


IO.Path.Combine takes care of a trailing path delimiter, or the
absence of one, for you.

Path.Combine("C:\website", "data") gives C:\website\data
Path.Combine("C:\website\", "data") also gives C:\website\data

Incidentally, if you use HttpRuntime.AppDomainAppPath instead of
Server.MapPath("~"), do you consistently get the trailing backslash?

Andrew

So we see the same thing: on the dev machine we don't get the trailing
slash.

However, HttpRuntime.AppDomainAppPath gives me the trailing slash both on
the dev maxhine and on the ISP.

BTW. I looked at Path.Combine on the Internet after yo mentioned it and saw
that:
Path.Combine("C:\website", "\TEST") returns \TEST


Thanks
 
I'm having a problem understanding the difference between application-root
and site-root.

Thanks

Not really a difference, I probably should have said "application
root".
The point is that in a live situation your application probably has
it's own url (www.somesite.com, leading to a "/" for
Server.MapPath("~")) while on you dev machine you have several
applications running (localhost/webapp1, localhost/webapp2, leading to
"/webapp1" or "/webapp2").

We built our own utility function around Request.ApplicationPath (which
returns the same value as Server.MapPath("~")), that returns an empty
string instead of a single "/" and leaves other values alone.

Hans Kesting
 
AAaron123 said:
So we see the same thing: on the dev machine we don't get the trailing
slash.

However, HttpRuntime.AppDomainAppPath gives me the trailing slash
both on the dev maxhine and on the ISP.

Well, at least /that's/ consistent, which reduces the problem-space.
BTW. I looked at Path.Combine on the Internet after yo mentioned it
and saw that:
Path.Combine("C:\website", "\TEST") returns \TEST

Yes, you shouldn't prepend the \ to the second argument.

Andrew
 
Hans said:
Not really a difference, I probably should have said "application
root".
The point is that in a live situation your application probably has
it's own url (www.somesite.com, leading to a "/" for
Server.MapPath("~")) while on you dev machine you have several
applications running (localhost/webapp1, localhost/webapp2, leading to
"/webapp1" or "/webapp2").

We built our own utility function around Request.ApplicationPath
(which returns the same value as Server.MapPath("~")), that returns
an empty string instead of a single "/" and leaves other values alone.

Hans Kesting

Thanks much
 
Back
Top