谁能解释一下 Server.MapPath(".")
、Server.MapPath("~")
、Server.MapPath(@"之间的区别\")
和 Server.MapPath("/")
?
最佳答案
Server.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
如果 Path 以正斜杠 (/
) 或反斜杠 (\
) 开头,则 MapPath()
返回路径为如果 Path 是完整的虚拟路径。
如果 Path 不以斜杠开头,则 MapPath()
返回相对于正在处理的请求目录的路径。
注意:在 C# 中,@
是逐字文字字符串运算符,这意味着该字符串应“按原样”使用,而不是针对转义序列进行处理。
脚注
Server.MapPath(null)
和Server.MapPath("")
将 produce this effect too .
关于asp.net - 服务器.MapPath ("."),服务器.MapPath ("~"),服务器.MapPath(@"\"),服务器.MapPath ("/")。有什么不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/275781/