网站在服务器磁盘上的物理路径: HttpRuntime.AppDomainAppPath虚拟程序路径: HttpRuntime.AppDomainAppVirtualPath
任何于Request/HttpContext.Current等相关的方法, 都只能在有请求上下文或者页面时使用. 即在无请求上下文时,HttpContext.Current为null. 而上面提到的方法一直可用.
对于全局Cache对象的访问亦然.
==================================================================================
示例:输出asp.net 网站路径。
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
![ASP.NET获取网站路径的最佳方法 - 醒悟 - 醒悟工作室](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
在ASP.NET编程中经常需要用Request获取url的有关信息.测试的url地址是http://www.test.com/testweb/default.aspx, 结果如下:
Request.ApplicationPath: /testweb
Request.CurrentExecutionFilePath: /testweb/default.aspxRequest.FilePath: /testweb/default.aspxRequest.Path: /testweb/default.aspxRequest.PathInfo:Request.PhysicalApplicationPath: E:\WWW\testweb\Request.PhysicalPath: E:\WWW\testweb\default.aspxRequest.RawUrl: /testweb/default.aspxRequest.Url.AbsolutePath: /testweb/default.aspxRequest.Url.AbsoluteUri: http://www.test.com/testweb/default.aspxRequest.Url.Host: www.test.comRequest.Url.LocalPath: /testweb/default.aspx当url中带参数时可以使用:
HttpContext.Current.Request.Url.PathAndQuery.ToString()//本页地址: Request.URL;
上页地址: Request.UrlReferrer Request.ServerViables["http_referer"] Request.RawUrl Request.RawUrl.QueryAndPath System.IO.Path.GetFileName(Request.FilePath.ToString())
HttpRequest 类型公开了以下成员。
构造函数名称 说明HttpRequest 基础结构。初始化 HttpRequest 对象。页首方法名称 说明BinaryRead 执行对当前输入流进行指定字节数的二进制读取。Equals 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。)Finalize 允许 Object 在“垃圾回收”回收 Object 之前尝试释放资源并执行其他清理操作。 (继承自 Object。)GetHashCode 用作特定类型的哈希函数。 (继承自 Object。)GetType 获取当前实例的 Type。 (继承自 Object。)MapImageCoordinates 将传入图像字段窗体参数映射为适当的 x 坐标值和 y 坐标值。MapPath 已重载。 为当前请求将请求的 URL 中的虚拟路径映射到服务器上的物理路径。MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)SaveAs 将 HTTP 请求保存到磁盘。ToString 返回表示当前 Object 的 String。 (继承自 Object。)ValidateInput 对通过 Cookies 、Form 和 QueryString 属性访问的集合进行验证。页首属性名称 说明AcceptTypes 获取客户端支持的 MIME 接受类型的字符串数组。AnonymousID 获取该用户的匿名标识符(如果存在)。ApplicationPath 获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。AppRelativeCurrentExecutionFilePath 获取应用程序根的虚拟路径,并通过对应用程序根使用波形符 (~) 表示法(例如,以“~/page.aspx”的形式)使该路径成为相对路径。Browser 获取或设置有关正在请求的客户端的浏览器功能的信息。ClientCertificate 获取当前请求的客户端安全证书。ContentEncoding 获取或设置实体主体的字符集。ContentLength 指定客户端发送的内容长度(以字节计)。ContentType 获取或设置传入请求的 MIME 内容类型。Cookies 获取客户端发送的 Cookie 的集合。CurrentExecutionFilePath 获取当前请求的虚拟路径。FilePath 获取当前请求的虚拟路径。Files 获取采用多部分 MIME 格式的由客户端上载的文件的集合。Filter 获取或设置在读取当前输入流时要使用的筛选器。Form 获取窗体变量集合。Headers 获取 HTTP 头集合。HttpMethod 获取客户端使用的 HTTP 数据传输方法(如 GET、POST 或 HEAD)。InputStream 获取传入的 HTTP 实体主体的内容。IsAuthenticated 获取一个值,该值指示是否验证了请求。IsLocal 获取一个值,该值指示该请求是否来自本地计算机。IsSecureConnection 获取一个值,该值指示 HTTP 连接是否使用安全套接字(即 HTTPS)。Item 从 Cookies、Form、QueryString 或 ServerVariables 集合中获取指定的对象。LogonUserIdentity 获取当前用户的 WindowsIdentity 类型。Params 获取 QueryString、Form、ServerVariables 和 Cookies 项的组合集合。Path 获取当前请求的虚拟路径。PathInfo 获取具有 URL 扩展名的资源的附加路径信息。PhysicalApplicationPath 获取当前正在执行的服务器应用程序的根目录的物理文件系统路径。PhysicalPath 获取与请求的 URL 相对应的物理文件系统路径。QueryString 获取 HTTP 查询字符串变量集合。RawUrl 获取当前请求的原始 URL。RequestType 获取或设置客户端使用的 HTTP 数据传输方法(GET 或 POST)。ServerVariables 获取 Web 服务器变量的集合。TotalBytes 获取当前输入流中的字节数。Url 获取有关当前请求的 URL 的信息。UrlReferrer 获取有关客户端上次请求的 URL 的信息,该请求链接到当前的 URL。UserAgent 获取客户端浏览器的原始用户代理信息。UserHostAddress 获取远程客户端的 IP 主机地址。UserHostName 获取远程客户端的 DNS 名称。UserLanguages 获取客户端语言首选项的排序字符串数组。
1.Request.ApplicationPath->当前应用的目录
楼主没有接触过jsp吧,如果接触过jsp就会非常清楚,ApplicationPath指的是当前的application(应用程序)的目录对应的--例如我的服务器上有两个web应用域名都是MySite.com 一个映射到目录MySite.com/1/ 另一个影射到 http://MySite.com/2/那么 MySite.com/1/就是第一个应用的ApplicationPath 同理 MySite.com/2/就是第二个应用的ApplicationPath2.Request.FilePath->对应于iis的虚拟目录如 URL http://MySite.com/1/index.html/pathinfoFilePath = /1/index.html3.Request.Path->当前请求的虚拟路径Path 是 FilePath 和 PathInfo 尾部的串联。例如 URL http://MySite.com/1/index.html/pathinfo那么Path = /1/index.html/pathinfo4.Request.MapPath(string url)->将url映射为iis上的虚拟目录这个目录都是相对于application的根目录的于Server.MapPath相比,不会包含类似c:/这样的路径可以理解为是相对路径(对比的Server.MapPath就是绝对路径)5.Server.MapPath(string url)->将url映射为服务器上的物理路径例如 http://MySite.com/1/index.html 假设你的应用程序在c:/iis/MySite中那么 就是 c:/iis/MySite/1/index.html
Requst.FilePath获取当前请求的虚拟路径。FilePath 属性不包含 PathInfo 尾部。例如,对于 URL http://www.contoso.com/virdir/page.html/tail,FilePath 值为 /virdir/page.html。
===============================================================================================================================Request.Path 获取当前请求的虚拟路径。Path 是 FilePath 和 PathInfo 尾部的串联。例如,对于 URL http://www.contoso.com/virdir/page.html/tail,Path 为 /virdir/page.html/tail。===============================================================================================================================Request.MapPath 方法将指定的虚拟路径映射到物理路径。下面的代码示例使用 MapPath 方法将虚拟路径转换为服务器上完全限定的物理路径。此示例包括两个部分:.aspx 页映射路径、读取文件并显示读取操作的结果。UpperCaseFilterStream 类,该类将通过它传递的所有字符都更改为大写。示例的第一部分显示如何使用 MapPath 方法将虚拟路径转换为完全限定的物理路径。之后,将此物理路径传递给 StreamReader 对象,该对象包含该文件的内容。然后,调用 Write 方法,以在页面上显示文件的内容。Filter 属性用于将筛选器附加到响应流,该响应流使页面上显示的文本全部大写。