Can anyone explain the difference between Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"") and Server.MapPath("/")?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Server.MapPath specifies the relative or virtual path to map to a physical directory.
Server.MapPath(".")1 returns the current physical directory of the file (e.g. aspx) being executedServer.MapPath("..")returns the parent directoryServer.MapPath("~")returns the physical path to the root of the applicationServer.MapPath("/")returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)
An example:
Let’s say you pointed a web site application (http://www.example.com/) to
C:Inetpubwwwroot
and installed your shop application (sub web as virtual directory in IIS, marked as application) in
D:WebAppsshop
For example, if you call Server.MapPath() in following request:
http://www.example.com/shop/products/GetProduct.aspx?id=2342
then:
Server.MapPath(".")1 returnsD:WebAppsshopproductsServer.MapPath("..")returnsD:WebAppsshopServer.MapPath("~")returnsD:WebAppsshopServer.MapPath("/")returnsC:InetpubwwwrootServer.MapPath("/shop")returnsD:WebAppsshop
If Path starts with either a forward slash (/) or backward slash (), the MapPath() returns a path as if Path was a full, virtual path.
If Path doesn’t start with a slash, the MapPath() returns a path relative to the directory of the request being processed.
Note: in C#, @ is the verbatim literal string operator meaning that the string should be used “as is” and not be processed for escape sequences.
Footnotes
Server.MapPath(null)andServer.MapPath("")will produce this effect too.
Method 2
Just to expand on @splattne’s answer a little:
MapPath(string virtualPath) calls the following:
public string MapPath(string virtualPath)
{
return this.MapPath(VirtualPath.CreateAllowNull(virtualPath));
}
MapPath(VirtualPath virtualPath) in turn calls MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, bool allowCrossAppMapping) which contains the following:
//...
if (virtualPath == null)
{
virtualPath = VirtualPath.Create(".");
}
//...
So if you call MapPath(null) or MapPath(""), you are effectively calling MapPath(".")
Method 3
1) Server.MapPath(".") — Returns the “Current Physical Directory” of the file (e.g. aspx) being executed.
Ex. Suppose D:WebApplicationsCollageDepartments
2) Server.MapPath("..") — Returns the “Parent Directory”
Ex. D:WebApplicationsCollage
3) Server.MapPath("~") — Returns the “Physical Path to the Root of the Application”
Ex. D:WebApplicationsCollage
4) Server.MapPath("/") — Returns the physical path to the root of the Domain Name
Ex. C:Inetpubwwwroot
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0