Server.MapPath(“.”)、Server.MapPath(“~”)、Server.MapPath(@“\”)、Server.MapPath(“/”)。 有什么不同?

发布于 2024-07-08 06:40:47 字数 154 浏览 6 评论 0原文

谁能解释一下 Server.MapPath(".")Server.MapPath("~")Server.MapPath(@"\") 之间的区别Server.MapPath("/")

Can anyone explain the difference between Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"\") and Server.MapPath("/")?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青春有你 2024-07-15 06:40:47

Server.MapPath 指定映射到物理目录的相对路径或虚拟路径。

  • Server.MapPath(".")1 返回正在执行的文件(例如 aspx)的当前物理目录
  • Server.MapPath("..")< /code> 返回父目录
  • 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# 中,@ 是逐字字符串运算符,这意味着该字符串应“按原样”使用,而不是针对转义序列进行处理。

脚注< /em>

  1. Server.MapPath(null)Server.MapPath("")

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 executed
  • Server.MapPath("..") returns the parent directory
  • Server.MapPath("~") returns the physical path to the root of the application
  • Server.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/) to

C:\Inetpub\wwwroot

and installed your shop application (sub web as virtual directory in IIS, marked as application) in

D:\WebApps\shop

For example, if you call Server.MapPath() in following request:

http://www.example.com/shop/products/GetProduct.aspx?id=2342

then:

  • Server.MapPath(".")1 returns D:\WebApps\shop\products
  • Server.MapPath("..") returns D:\WebApps\shop
  • Server.MapPath("~") returns D:\WebApps\shop
  • Server.MapPath("/") returns C:\Inetpub\wwwroot
  • Server.MapPath("/shop") returns D:\WebApps\shop

If Path starts with either a forward slash (/) or backward slash (\), the MapPath() 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

  1. Server.MapPath(null) and Server.MapPath("") will produce this effect too.
简单爱 2024-07-15 06:40:47

只是稍微扩展一下@splattne的答案:

MapPath(string virtualPath) 调用以下内容:

public string MapPath(string virtualPath)
{
    return this.MapPath(VirtualPath.CreateAllowNull(virtualPath));
}

MapPath(VirtualPath virtualPath) 依次调用 MapPath(VirtualPath virtualPath, VirtualPath) baseVirtualDir, bool allowedCrossAppMapping) 其中包含以下内容:

//...
if (virtualPath == null)
{
    virtualPath = VirtualPath.Create(".");
}
//...

因此,如果您调用 MapPath(null)MapPath(""),则实际上是在调用 地图路径(“。”)

Just to expand on @splattne's answer a little:

MapPath(string virtualPath) calls the following:

public string MapPath(string virtualPath)
{
    return this.MapPath(VirtualPath.CreateAllowNull(virtualPath));
}

MapPath(VirtualPath virtualPath) in turn calls MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, bool allowCrossAppMapping) which contains the following:

//...
if (virtualPath == null)
{
    virtualPath = VirtualPath.Create(".");
}
//...

So if you call MapPath(null) or MapPath(""), you are effectively calling MapPath(".")

他夏了夏天 2024-07-15 06:40:47

1) Server.MapPath(".") -- 返回正在执行的文件的“当前物理目录”(例如aspx)。

前任。 假设 D:\WebApplications\Collage\Departments

2) Server.MapPath("..") -- 返回“父目录

D:\WebApplications\Collage

的物理路径”

3) Server.MapPath("~") -- 返回“应用程序根目录 D:\WebApplications\Collage

4) Server.MapPath("/") -- 返回域名

Ex 根目录的物理路径。 C:\Inetpub\wwwroot

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 Name

Ex. C:\Inetpub\wwwroot

剩余の解释 2024-07-15 06:40:47

工作示例,希望这有助于展示一种使用 MapPath 而不仅仅是“/”的方法。 我们将“/”和“~”组合起来。

  1. 字符串lotMapsUrl =“〜/ WebFS / Transport / Maps / Lots /”; ---只需获取
    长 URL 转换为变量
  2. 字符串 lotMapsDir = Server.MapPath(lotMapsUrl); ---得到我们的充分
    此位置的物理路径
  3. string[] files = Directory.GetFiles(lotMapsUrl); ---去拿一份清单
    物理路径中的文件。

Working Example, hope this helps show a way to use MapPath with more than just a "/". We are combining "/" and "~".

  1. string lotMapsUrl = "~/WebFS/Transport/Maps/Lots/"; --- just get the
    long URL into a variable
  2. string lotMapsDir = Server.MapPath(lotMapsUrl); --- get our full
    physical path to this location
  3. string[] files = Directory.GetFiles(lotMapsUrl); --- go grab a list
    of the files from the physical path.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文