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

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
 
A

AAaron123

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.
 
H

Hans Kesting

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
 
A

AAaron123

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
 
A

Andrew Morton

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
 
A

AAaron123

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
 
A

Andrew Morton

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
 
A

AAaron123

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
 
H

Hans Kesting

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
 
A

Andrew Morton

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
 
A

AAaron123

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
 
A

AAaron123

Andrew said:
Well, at least /that's/ consistent, which reduces the problem-space.


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

Andrew

Thanks
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top