request.mappath , request.physicalapplicationpath and Server.mappath
来源:互联网 发布:中国数据域名管理平台 编辑:程序博客网 时间:2024/05/18 01:27
ASP.NET中Server.MapPath() 和 Request.MapPath() 与Request.PhysicalApplicationPath
Response.Write(Request.PhysicalApplicationPath.ToString());
Response.Write(Server.MapPath("mydb.mdb").ToString());
经过实验,我发现:Request.PhysicalApplicationPath.ToString()是你的网站所在的目录 ,比如我的就是F:/fq/htdocs/,而这个地址不管你的网页在哪个子文件夹里,都是一样的。
Server.MapPath("mydb.mdb").ToString() 是你的网页所在的文件夹再加上参数字符串,而不管这个参数字符串是什么,存在不存在 比如我在qiye子文件夹里放了一个1/aspx文件,其中有这样一句:Response.Write(Server.MapPath("mydb.mdb").ToString());试验出来的值是:F:/fq/htdocs/qiye/mydb.mdb
另外,Server.MapPath("mydb.mdb").的参数的首部最好不要含有“/”,l因为这意味着根。Response.Write(Server.MapPath("/mydb.mdb").ToString());试验出来的值是:c:/www_root
ASP.NET中Server.MapPath() 和 Request.MapPath()
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)就可以调用同一个目录文件。不用做目录判断
1、Server.MapPath("/") 应用程序根目录所在的位置 如 C:/Inetpub/wwwroot/
2、Server.MapPath("./") 表示所在页面的当前目录
注:等价于Server.MapPath("") 返回 Server.MapPath("") 所在页面的物理文件路径
3、Server.MapPath("../")表示上一级目录
4、Server.MapPath("~/")表示当前应用级程序的目录,如果是根目录,就是根目录,如果是虚拟目录,就是虚拟目录所在的位置 如 C:/Inetpub/wwwroot/Example/
很多人对它们都不陌生,在众多的WEB程序中,使用Server.MapPath和Request.PhysicalApplicationPath来操作目录/文件的几率参半,我曾经也经常混用,然而时间久了。发现Request.PhysicalApplicationPath有点“麻烦”?
其实在一般的应用程序中,感觉不到两者的区别,而在一些存在“虚拟目录”、存在多级子目录、需要虚拟目录相互文件操作的站点中,就可以知道它们的区别了。我的总结如下:
1、相对当前路径的文件操作:
可以使用Server.MapPath("test.txt")、Server.MapPath("a/test.txt")、Server.MapPath("../b/test.txt"),这个时候使用Request.PhysicalApplicationPath就很难定位到文件;
2、绝对路径的文件操作:
这个时候两者都可以,有点注意的是Server.MapPath中的绝对是“面向”站点而言的,不会因为“虚拟目录”本身的属性不同而变化,Request.PhysicalApplicationPath就并非如此。
打个比方:主站点指向D:/wwwroot/,虚拟目录指向E:/wwwroot/a/,取名为a。
那么如果我要操作虚拟目录下的E:/wwwroot/a/test.txt,可以使用Server.MapPath("/a/test.txt"),又或者直接使用Server.MapPath("~/test.txt");
那么用Request.PhysicalApplicationPath该怎么写?
很多人数是:Request.PhysicalApplicationPath + "test.txt",我说不一定,为什么?
因为要是该虚拟目录有单独的应用池,Request.PhysicalApplicationPath就是E:/wwwroot/a/,后面加上test.txt就是E:/wwwroot/a/test.txt;
但要是该虚拟目录没有单独的应用池,Request.PhysicalApplicationPath就是D:/wwwroot/,不管你后面怎么加,都定位不到我要找到文件了。呵呵……
3、跨虚拟目录的文件操作:
比如我现在要操作虚拟目录b下的test2.txt文件,使用Server.MapPath("/b/test2.txt")就没有问题(前提是我有操作权限,没有权限是另外一回事)。
而Request.PhysicalApplicationPath根本就无法找到其他的虚拟目录下的文件,不信谁试试,再告诉我方法?!!!
综上所述,我个人以后更倾向使用Server.MapPath。
Response.Write(Request.PhysicalApplicationPath.ToString());
Response.Write(Server.MapPath("mydb.mdb").ToString());
经过实验,我发现:Request.PhysicalApplicationPath.ToString()是你的网站所在的目录 ,比如我的就是F:/fq/htdocs/,而这个地址不管你的网页在哪个子文件夹里,都是一样的。
Server.MapPath("mydb.mdb").ToString() 是你的网页所在的文件夹再加上参数字符串,而不管这个参数字符串是什么,存在不存在 比如我在qiye子文件夹里放了一个1/aspx文件,其中有这样一句:Response.Write(Server.MapPath("mydb.mdb").ToString());试验出来的值是:F:/fq/htdocs/qiye/mydb.mdb
另外,Server.MapPath("mydb.mdb").的参数的首部最好不要含有“/”,l因为这意味着根。Response.Write(Server.MapPath("/mydb.mdb").ToString());试验出来的值是:c:/www_root
ASP.NET中Server.MapPath() 和 Request.MapPath()
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)就可以调用同一个目录文件。不用做目录判断
1、Server.MapPath("/") 应用程序根目录所在的位置 如 C:/Inetpub/wwwroot/
2、Server.MapPath("./") 表示所在页面的当前目录
注:等价于Server.MapPath("") 返回 Server.MapPath("") 所在页面的物理文件路径
3、Server.MapPath("../")表示上一级目录
4、Server.MapPath("~/")表示当前应用级程序的目录,如果是根目录,就是根目录,如果是虚拟目录,就是虚拟目录所在的位置 如 C:/Inetpub/wwwroot/Example/
字号:大 中 小 ASP.NET中Server.MapPath() 和 Request.MapPath()使用区别: ASP.NET中Server.MapPath() 和 Request.MapPath()使用区别:2006-12-20 11:12http://hi.baidu.com/zj53haoASP.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)就可以调用同一个目录文件。不用做目录判断
- request.mappath , request.physicalapplicationpath and Server.mappath
- 浅谈Server.MapPath和Request.PhysicalApplicationPath
- Server.MapPath和Request.MapPath()的用法
- Server.MapPath和Request.MapPath()的用法
- Server.MapPath() 和 Request.MapPath()使用区别
- ASP.NET中Server.MapPath() 和Request.MapPath()使用
- ASP.NET中Server.MapPath() 和Request.MapPath()使用
- ASP.NET中Request.ApplicationPath、Request.FilePath、Request.Path、.Request.MapPath、Server.MapPath的区别
- ASP.NET中Request.ApplicationPath、Request.FilePath、Request.Path、.Request.MapPath、Server.MapPath的区别
- mappath使用介绍,Server.mappath和Request.mappath 有什么区别、及使用方法
- ASP.NET中Server.MapPath() 和 Request.MapPath()使用区别(于三份资料中提取)
- Server.MapPath()
- Server.MapPath
- Server.MapPath
- Server.MapPath()
- Server.MapPath()
- Server.MapPath
- Server.MapPath(".") Server.MapPath("./") Server.MapPath("/") Server.MapPath用法详解
- 删除表中重复记录
- 哎@@哎
- WizKnowledge的插入代码插件
- Jess 7.0p1——Java平台规则引擎(一)
- 我的CSDN
- request.mappath , request.physicalapplicationpath and Server.mappath
- 我看MFC的过去、现在和未来
- (转)十年学会程序设计
- SQLserver2005 类别不支持集合(或类别对象为远程对象) (异常来自 HRESULT:0x80040110 (CLASS_E_NOAGGREGATION)) (Microsoft.SqlServer.SqlTools.VSIntegration)
- 【linux】c++ 内存管理(三)
- 如何修复损坏的硬盘分区
- 最近的杰作
- 求注册码
- 新工作