Can anyone explain the difference between Server.MapPath(".")
, Server.MapPath("~")
, Server.MapPath(@"\")
and Server.MapPath("/")
?
相关问题
- Carriage Return (ASCII chr 13) is missing from tex
- How to store image outside of the website's ro
- 'System.Threading.ThreadAbortException' in
- How to access the camera from my Windows Phone 8 a
- Request.PathInfo issues and XSS attacks
相关文章
- asp.net HiddenField控件扩展问题
- asp.net HiddenField控件扩展问题
- Asp.Net网站无法写入错误日志,测试站点可以,正是站点不行
- asp.net mvc 重定向到vue hash字符串丢失
- FormsAuthenticationTicket expires too soon
- “Dynamic operations can only be performed in homog
- What is the best way to create a lock from a web a
- Add to htmlAttributes for custom ActionLink helper
Just to expand on @splattne's answer a little:
MapPath(string virtualPath)
calls the following:MapPath(VirtualPath virtualPath)
in turn callsMapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, bool allowCrossAppMapping)
which contains the following:So if you call
MapPath(null)
orMapPath("")
, you are effectively callingMapPath(".")
1)
Server.MapPath(".")
-- Returns the "Current Physical Directory" of the file (e.g.aspx
) being executed.Ex. Suppose
D:\WebApplications\Collage\Departments
2)
Server.MapPath("..")
-- Returns the "Parent Directory"Ex.
D:\WebApplications\Collage
3)
Server.MapPath("~")
-- Returns the "Physical Path to the Root of the Application"Ex.
D:\WebApplications\Collage
4)
Server.MapPath("/")
-- Returns the physical path to the root of the Domain NameEx.
C:\Inetpub\wwwroot
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\products
Server.MapPath("..")
returnsD:\WebApps\shop
Server.MapPath("~")
returnsD:\WebApps\shop
Server.MapPath("/")
returnsC:\Inetpub\wwwroot
Server.MapPath("/shop")
returnsD:\WebApps\shop
If 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.