Server.MapPath(“.")、Server.MapPath(“~")、Server.MapPath(@“")、Server.MapPath(“/").

编程入门 行业动态 更新时间:2024-10-24 12:31:25
本文介绍了Server.MapPath(“.")、Server.MapPath(“~")、Server.MapPath(@“")、Server.MapPath(“/").有什么不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

谁能解释一下Server.MapPath(".")、Server.MapPath("~")、Server.MapPath(@"") 和 Server.MapPath("/")?

解决方案

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

  • Server.MapPath(".")1 返回正在执行的文件(例如 aspx)的当前物理目录
  • Server.MapPath("..") 返回父目录
  • Server.MapPath("~") 返回应用程序根目录的物理路径
  • Server.MapPath("/") 返回域名根的物理路径(不一定与应用的根相同)

示例:

假设您将一个网站应用程序 (www.example/) 指向

C:Inetpubwwwroot

并在

中安装您的商店应用程序(子网站作为IIS中的虚拟目录,标记为应用程序)

D:WebAppsshop

例如,如果您在以下请求中调用 Server.MapPath():

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

然后:

  • Server.MapPath(".")1 返回 D:WebAppsshopproducts
  • Server.MapPath("..") 返回 D:WebAppsshop
  • Server.MapPath("~") 返回 D:WebAppsshop
  • Server.MapPath("/") 返回 C:Inetpubwwwroot
  • Server.MapPath("/shop") 返回 D:WebAppsshop

如果 Path 以正斜杠 (/) 或反斜杠 () 开头,则 MapPath() 返回路径为如果 Path 是完整的虚拟路径.

如果 Path 不以斜杠开头,MapPath() 将返回一个相对于正在处理的请求的目录的路径.

注意:在 C# 中,@ 是逐字文本字符串运算符,这意味着该字符串应该按原样"使用,而不是针对转义序列进行处理.>

脚注

  • Server.MapPath(null) 和 Server.MapPath("") 将 也会产生这种效果.
  • Can anyone explain the difference between Server.MapPath("."), Server.MapPath("~"), Server.MapPath(@"") and 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 (www.example/) to

    C:Inetpubwwwroot

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

    D:WebAppsshop

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

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

    then:

    • Server.MapPath(".")1 returns D:WebAppsshopproducts
    • Server.MapPath("..") returns D:WebAppsshop
    • Server.MapPath("~") returns D:WebAppsshop
    • Server.MapPath("/") returns C:Inetpubwwwroot
    • Server.MapPath("/shop") returns D:WebAppsshop

    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

  • Server.MapPath(null) and Server.MapPath("") will produce this effect too.
  • 更多推荐

    Server.MapPath(“.")、Server.MapPath(“~")、Server.MapPath(@“")、Serve

    本文发布于:2023-11-09 15:52:42,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1572784.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:有什么不同   Server   MapPath   quot

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!