Server.MapPath()的用法

http://blog.csdn.net/qiuhaifeng_csu/article/details/19416407 

Server.MapPath(string path)作用是返回与Web服务器上的指定虚拟路径相对应的物理文件路径。其参数path为Web 服务器的虚拟路径返回结果是与path相对应的物理文件路径。但有时参数并非为虚拟路径,而是用户自定义的文件名。

    Server.MapPath()的全名是System.Web.HttpContext.Current.Server.MapPath()。有时在程序调试时会提示“当前上下文中不存在名称“Server””错误,从而不支持函数Server.MapPath()的使用。尽管引用了命名空间“using System.Web;”也是无济于事,此时就需要使用其全名,或者是当前使用Server.MapPath()函数的类继承自System.Web.UI.Page。

Server.MapPath("") :返回当前页面所在的物理文件路径

Server.MapPath("/") :返回应用程序根目录所在的物理文件路径

Server.MapPath("./") :返回当前页面所在的物理文件路径

Server.MapPath("../"):返回当前页面所在的上一级的物理文件路径

Server.MapPath("~/"):返回应用程序的虚拟目录(路径)

Server.MapPath("~"):返回应用程序的虚拟目录(路径)

说明:对于Server.MapPath()具体返回什么内容,在不同的环境下得到的结果也许并不相同

用法:
1.Server.MapPath("/")  应用程序根目录所在的位置 如 C:Inetpubwwwroot

2.Server.MapPath("./")  表示所在页面的当前目录 ( 注:等价于Server.MapPath("")  返回 Server.MapPath("")所在页面的物理文件路径)

3.Server.MapPath("../")表示上一级目录 

4.Server.MapPath("~/")表示当前应用级程序的目录

:Server.MapPath("~") + @"/Content/Data/LeftMenu.xml";

 "D:\Project\TMS2012\TMSWeb" 

    如果是根目录,就是根目录;

    如果是虚拟目录,就是虚拟目录所在的位置 如:C:InetpubwwwrootExample

    注:等效于Server.MapPath("~")。


当前的网站目录为E:wwwroot   

应用程序虚拟目录为E:wwwrootcompany 

浏览的页面路径为E:wwwrootcompany ewsshow.asp

在show.asp页面中使用

Server.MapPath("./")   返回路径为:E:wwwrootcompany ews

Server.MapPath("/")    返回路径为:E:wwwroot

Server.MapPath("../")   返回路径为:E:wwwrootcompany

Server.MapPath("~/")   返回路径为:E:wwwrootcompany

server.MapPath(request.ServerVariables("Path_Info")) 

Request.ServerVariables("Path_Translated")  

上面两种方式返回路径为 D:wwwrootcompany ewsshow.asp

原文地址:https://www.cnblogs.com/ZkbFighting/p/8399997.html