码迷,mamicode.com
首页 > 移动开发 > 详细

Server.MapPath()用法

时间:2018-12-11 12:44:49      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:example   应用   文件路径   环境   tran   一级目录   ring   命名   命名空间   

   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()用法

标签:example   应用   文件路径   环境   tran   一级目录   ring   命名   命名空间   

原文地址:https://www.cnblogs.com/oralig/p/10101200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!