[转载]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)就可以调用同一个目录文件。不用做目录判断
./当前目录 
/根目录 
../上层目录(相对当前来说) 
如果当前的网站目录为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)就可以调用同一个目录文件。不用做目录判断

猜你喜欢

转载自www.cnblogs.com/hx215267863/p/12048880.html
今日推荐