• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
梦天涯#
博客园    首页    新随笔    联系   管理    订阅  订阅

[转载]Server.MapPath和Request.MapPath()的用法

Server.MapPath和Request.MapPath()的用法

 

转载自:https://blog.csdn.net/blocksom/article/details/7785570

 
./当前目录 
/根目录 
../上层目录(相对当前来说) 
如果当前的网站目录为D:\wwwroot    浏览的页面路径为D:\wwwroot\company\news\show.asp 
在show.asp页面中使用
Server.MapPath("./")   返回路径为:D:\wwwroot\company\news 
Server.MapPath("/")    返回路径为:D:\wwwroot 
Server.MapPath("../")   返回路径为:D:\wwwroot\company 
server.MapPath(request.ServerVariables("Path_Info")) 
Request.ServerVariables("Path_Translated")   
上面两种方式返回路径为 D:\wwwroot\company\news\show.asp 


ASP.NET中Server.MapPath() 和 Request.MapPath()区别: 
Server.MapPath(string)   :是将相对于当前调用文件的文件(或目录)映射为物理路径; 
Request.MapPath(string) :是将string虚拟路径映射为物理路径(asp中Request无此方法) 
Server.MapPath(string) 中string 可以用“../”方式引用父目录,甚至可以将此目录跳到整个WEB目录外,如:C:\WWWROOT 
目录为WEB根目录,在根目录文件中调用此Server.MapPath("../xyz.gif"),则可以调用WEB目录外的脚本、资源等。 
Request.MapPath(string) 中的string为虚拟目录,只能相对WEB虚拟目录形式的,也不允许"../"方式调用,只能是"/","/xx"等字符串 
有时候直接用Server.MapPath(string) 调用一个文件比较麻烦,因为不同的目录中调用同一个Server.MapPath(string) 函数就会得到不同的值, 
特殊的话,就需要通过判断本身目录层次才能获取正确的地址,使用的Request.MapPath(string)就可以调用同一个目录文件。不用做目录判断
posted @ 2019-12-16 14:17  梦天涯#  阅读(239)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3