ASP.NET获取网站路径

2013 年 1 月 19 日3960

欢迎进入.NET社区论坛,与300万技术人员互动交流 >>进入

  网站在服务器磁盘上的物理路径: HttPRuntime.AppDomainAppPath

  虚拟程序路径: HttpRuntime.AppDomainAppVirtualPath

  任何于Request/HttpContext.Current等相关的方法, 都只能在有请求上下文或者页面时使用. 即在无请求上下文时,HttpContext.Current为null. 而上面提到的方法一直可用.

  对于全局Cache对象的访问亦然.

  示例:输出asp.net 网站路径。

  private void responseHtml()

  ...{

  System.Text.StringBuilder sb = new System.Text.StringBuilder();

  sb.Append(string.Format("当前时间: {0}", Server.HtmlEncode(DateTime.Now.ToString())));

  sb.Append("<br />");

  sb.Append(string.Format("当前请求的虚拟路径: {0}",Server.HtmlEncode(Request.CurrentExecutionFilePath)));

  sb.Append("<br />");

  sb.Append(string.Format("获取当前应用程序的根目录路径: {0}", Server.HtmlEncode(Request.applicationPath)));

  sb.Append("<br />");

  sb.Append(string.Format("当前请求的虚拟路径: {0}",Server.HtmlEncode(Request.FilePath)));

  sb.Append("<br />");

  sb.Append(string.Format("当前请求的虚拟路径: {0}",Server.HtmlEncode(Request.Path)));

  sb.Append("<br />");

  sb.Append(string.Format("获取当前正在执行的应用程序的根目录的物理文件系统路径: {0}", Server.HtmlEncode(Request.PhysicalApplicationPath)));

  sb.Append("<br />");

  sb.Append(string.Format("获取与请求的 URL 相对应的物理文件系统路径: {0}", Server.HtmlEncode(Request.PhysicalApplicationPath)));

  sb.Append("<br />");

  Response.Write(sb.ToString());

  }

  }

  输出:当前时间: 2007-08-30 11:03:49

  当前请求的虚拟路径: /aDirectory/Sample/responseHtml.aspx

  获取当前应用程序的根目录路径: /aDirectory

  当前请求的虚拟路径: /aDirectory/Sample/responseHtml.aspx

  当前请求的虚拟路径: /aDirectory/Sample/responseHtml.aspx

  获取当前正在执行的应用程序的根目录的物理文件系统路径: E:\Visual Studio 2005\

  获取与请求的 URL 相对应的物理文件系统路径: E:\Visual Studio 2005\\aDirectory\

  在ASP.NET编程中经常需要用Request获取url的有关信息.

  测试的url地址是.test/testweb/default.aspx, 结果如下:

  Request.ApplicationPath: /testweb

  Request.CurrentExecutionFilePath: /testweb/default.aspx

  Request.FilePath: /testweb/default.aspx

  Request.Path: /testweb/default.aspx

  Request.PathInfo:

  Request.PhysicalApplicationPath: E:\WWW\testweb\

  Request.PhysicalPath: E:\WWW\testweb\default.aspx

  Request.RawUrl: /testweb/default.aspx

  Request.Url.AbsolutePath: /testweb/default.aspx

  Request.Url.AbsoluteUri: /testweb/default.aspx

  Request.Url.Host:

  Request.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。)

[1][2]下一页

【责编:ivy】

0 0