服务器。MapPath("."), 服务器。MapPath("~")、服务器。MapPath(@"\")、服务器。MapPath("/").有什么区别?
-
07-07-2019 - |
题
任何人都可以解释之间的差异 Server.MapPath(".")
, Server.MapPath("~")
, Server.MapPath(@"\")
和 Server.MapPath("/")
?
解决方案
服务器。MapPath 指定相对的或虚拟的路径图 一个物理的目录.
Server.MapPath(".")
1 返回的当前物目录的文件(例如aspx)正在执行Server.MapPath("..")
返回母体目录Server.MapPath("~")
返回的物理路径的根源应用程序Server.MapPath("/")
返回的物理路径的根域名(不一定是相同的根本的应用程序)
一个例子:
让我们假设你指出一个网站应用程序(http://www.example.com/
)来
C:\Inetpub\wwwroot
并安装了你的店的应用程序(子网作为虚拟目录中IIS,标记为应用程序)中
D:\WebApps\shop
例如,如果你打电话 Server.MapPath()
在以下请求:
http://www.example.com/shop/products/GetProduct.aspx?id=2342
然后:
Server.MapPath(".")
1 返回D:\WebApps\shop\products
Server.MapPath("..")
返回D:\WebApps\shop
Server.MapPath("~")
返回D:\WebApps\shop
Server.MapPath("/")
返回C:\Inetpub\wwwroot
Server.MapPath("/shop")
返回D:\WebApps\shop
如果路开始用斜线(/
)或反斜杠(\
), MapPath()
返回的路径,如果路径是一个完整的、虚拟的路径。
如果路径没有开始削减, MapPath()
返回的路径相对于该目录的请求正在处理中。
注:在C# @
是的逐字记录的文字串的操作意义,串应使用"如"和不加以处理的逃生顺序。
脚注
Server.MapPath(null)
和Server.MapPath("")
将 产生这种效果太.
其他提示
稍微扩展@splattne的回答:
MapPath(string virtualPath)
调用以下内容:
public string MapPath(string virtualPath)
{
return this.MapPath(VirtualPath.CreateAllowNull(virtualPath));
}
MapPath(VirtualPath virtualPath)
依次调用 MapPath(VirtualPath virtualPath,VirtualPath baseVirtualDir,bool allowCrossAppMapping)
,其中包含以下内容:
//...
if (virtualPath == null)
{
virtualPath = VirtualPath.Create(".");
}
//...
因此,如果您调用 MapPath(null)
或 MapPath("")
,您实际上是在调用 MapPath("。")代码>
1) Server.MapPath("。")
- 返回“当前物理目录”。正在执行的文件(例如 aspx
)。
实施例。假设 D:\ WebApplications \ Collage \ Departments
2) Server.MapPath(" ..")
- 返回“父目录”
实施例。 <代码> d:\ web应用\拼贴代码>
3) Server.MapPath(&quot;〜&quot;)
- 返回“应用程序根目录的物理路径”
实施例。 <代码> d:\ web应用\拼贴代码>
4) Server.MapPath(&quot; /&quot;)
- 返回域名根目录的物理路径
实施例。 <代码> C:\的Inetpub \ wwwroot的代码>