Can anyone explain the difference between Server.MapPath('.'), Server.MapPath('~'), Server.MapPath(@'\') and Server.MapPath('/')?
Can anyone explain the difference between Server.MapPath(.) , Server.MapPath(~) , Server.MapPath(@\) and Server.MapPath(/) ?
Share
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/) toand installed your shop application (sub web as virtual directory in IIS, marked as application) in
For example, if you call
Server.MapPath()in following request:then:
Server.MapPath('.')1 returnsD:\WebApps\shop\productsServer.MapPath('..')returnsD:\WebApps\shopServer.MapPath('~')returnsD:\WebApps\shopServer.MapPath('/')returnsC:\Inetpub\wwwrootServer.MapPath('/shop')returnsD:\WebApps\shopIf Path starts with either a forward slash (
/) or backward slash (\), theMapPath()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.