Server.MapPath()用法
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:\\Inetpub\\wwwroot\\
2.Server.MapPath("./") 表示所在页面的当前目录 (注:等价于Server.MapPath("") 返回 Server.MapPath("")所在页面的物理文件路径)
3.Server.MapPath("../")表示上一级目录
4.Server.MapPath("~/")表示当前应用级程序的目录
如果是根目录,就是根目录;
如果是虚拟目录,就是虚拟目录所在的位置 如:C:\\Inetpub\\wwwroot\\Example\\
注:等效于Server.MapPath("~")。
当前的网站目录为E:\\wwwroot
应用程序虚拟目录为E:\\wwwroot\\company
浏览的页面路径为E:\\wwwroot\\company\\news\\show.asp
在show.asp页面中使用
Server.MapPath("./") 返回路径为:E:\\wwwroot\\company\\news
Server.MapPath("/") 返回路径为:E:\\wwwroot
Server.MapPath("../") 返回路径为:E:\\wwwroot\\company
Server.MapPath("~/") 返回路径为:E:\\wwwroot\\company
server.MapPath(request.ServerVariables("Path_Info"))
Request.ServerVariables("Path_Translated")
上面两种方式返回路径为 D:\\wwwroot\\company\\news\\show.asp
补充:Server.MapPath用法
Server.MapPath的使用方法了,下面记录一下,以备后用:总注:Server.MapPath获得的路径都是服务器上的物理路径,也就是常说的绝对路径1、Server.MapPath("/")注:获得应用程序根目录所在的位置,如 C:\\Inetpub\\wwwroot\\。2、Server.MapPath("./")注:获得所在页面的当前目录,等价于Server.MapPath("")。3、Server.MapPath("../")注:获得所在页面的上级目录。4、Server.MapPath("~/")注:获得当前应用级程序的目录,如果是根目录,就是根目录,如果是虚拟目录,就是虚拟目录所在的位置,如C:\\Inetpub\\wwwroot\\Example\\。
到此这篇关于ASP.Net中的Server.MapPath()用法的文章就介绍到这了,更多相关Server.MapPath()用法内容请搜索悠久资源网以前的文章或继续浏览下面的相关文章希望大家以后多多支持悠久资源网!
您可能感兴趣的文章:
- ASP的Server.MapPath()不同参数返回路径总结