Server.MapPath(“.”)、Server.MapPath(“~”)、Server.MapPath(@“\”)、Server.MapPath(“/”)。 有什么不同?
谁能解释一下 Server.MapPath(".")
、Server.MapPath("~")
、Server.MapPath(@"\") 之间的区别
和Server.MapPath("/")
?
Can anyone explain the difference between Server.MapPath(".")
, Server.MapPath("~")
, Server.MapPath(@"\")
and Server.MapPath("/")
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Server.MapPath 指定映射到物理目录的相对路径或虚拟路径。
Server.MapPath(".")
1 返回正在执行的文件(例如 aspx)的当前物理目录Server.MapPath("..")< /code> 返回父目录
Server.MapPath("~")
返回应用程序根目录的物理路径Server.MapPath("/")
返回应用程序根目录的物理 路径域名根的路径(不一定与应用程序的根相同)示例:
假设您指向一个网站应用程序 (
http://www.example .com/
)并安装您的商店应用程序(子网站作为 IIS 中的虚拟目录,标记为应用程序)
例如,如果您在以下请求中调用
Server.MapPath()
:然后:
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>
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 executedServer.MapPath("..")
returns the parent directoryServer.MapPath("~")
returns the physical path to the root of the applicationServer.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/
) toand installed your shop application (sub web as virtual directory in IIS, marked as application) in
For example, if you call
Server.MapPath()
in following request:then:
Server.MapPath(".")
1 returnsD:\WebApps\shop\products
Server.MapPath("..")
returnsD:\WebApps\shop
Server.MapPath("~")
returnsD:\WebApps\shop
Server.MapPath("/")
returnsC:\Inetpub\wwwroot
Server.MapPath("/shop")
returnsD:\WebApps\shop
If Path starts with either a forward slash (
/
) or backward slash (\
), theMapPath()
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)
andServer.MapPath("")
will produce this effect too.只是稍微扩展一下@splattne的答案:
MapPath(string virtualPath)
调用以下内容:MapPath(VirtualPath virtualPath)
依次调用MapPath(VirtualPath virtualPath, VirtualPath) baseVirtualDir, bool allowedCrossAppMapping)
其中包含以下内容:因此,如果您调用
MapPath(null)
或MapPath("")
,则实际上是在调用地图路径(“。”)
Just to expand on @splattne's answer a little:
MapPath(string virtualPath)
calls the following:MapPath(VirtualPath virtualPath)
in turn callsMapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, bool allowCrossAppMapping)
which contains the following:So if you call
MapPath(null)
orMapPath("")
, you are effectively callingMapPath(".")
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 NameEx.
C:\Inetpub\wwwroot
工作示例,希望这有助于展示一种使用 MapPath 而不仅仅是“/”的方法。 我们将“/”和“~”组合起来。
长 URL 转换为变量
此位置的物理路径
物理路径中的文件。
Working Example, hope this helps show a way to use MapPath with more than just a "/". We are combining "/" and "~".
long URL into a variable
physical path to this location
of the files from the physical path.