服务器。MapPath("."), 服务器。MapPath("~")、服务器。MapPath(@"\")、服务器。MapPath("/").有什么区别?

StackOverflow https://stackoverflow.com/questions/275781

任何人都可以解释之间的差异 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# @ 是的逐字记录的文字串的操作意义,串应使用"如"和不加以处理的逃生顺序。

脚注

  1. 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的

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top