Request.ApplicationPath
Request.FilePath
Request.PathRequest.MapPath()
Server.MapPath()以上到底有什么区别?最好能举例说明一下……假设我网站的域名是 http://MySite.com
在http://MySite.com/Sub1/Object1.aspx页面上调用以上的属性和方法分别能得到哪些结果?最主要还是想知道他们各自的作用和区别之处……先谢过回答的各位了~

解决方案 »

  1.   

    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  获取客户端语言首选项的排序字符串数组。 
    详细请见
    ms-help://MS.MSDNQTR.v90.chs/fxref_system.web/html/c0fbc05b-94af-d56d-5044-22d55ee789a7.htm
      

  2.   

    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/pathinfo
      FilePath = /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以上是个人理解,不知道是否全对
      

  3.   

    Request.ApplicationPath 获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。可从不在根目录中的页或 Web 用户控件中使用此属性构造一个相对于应用程序根目录的 URL。这样,位于其他目录结构级别的页和共享控件可以使用相同的代码链接到应用程序中位于固定位置的资源。
    下面的示例使用 ApplicationPath 属性,以编程方式构造一个指向位于应用程序中某个固定位置的资源的路径。引用该资源的页不必与该资源位于同一目录中。<%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">    protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Text = Request.ApplicationPath;
            Image1.ImageUrl = Request.ApplicationPath + "/images/Image1.gif";
            Label2.Text = Image1.ImageUrl;        Label3.Text = Request.AppRelativeCurrentExecutionFilePath;
            if (VirtualPathUtility.GetDirectory(
                Request.AppRelativeCurrentExecutionFilePath).Equals("~/Members/"))
            {
                Image2.ImageUrl = Request.ApplicationPath +
                    "/memberimages/Image1.gif";
            }
            else
            {
                Image2.ImageUrl = Request.ApplicationPath +
                "/guestimages/Image1.gif";
            }
            Label4.Text = Image2.ImageUrl;
        }
    </script><html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>HttpRequest.ApplicationPath Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            This is the ApplicationPath from the current page:<br />
            <asp:Label ID="Label1" runat="server" ForeColor="Brown" /><br />
            Use it to link to resources at fixed locations in the application.<br />
            <asp:Image ID="Image1" runat="server" />
            <asp:Label ID="Label2" runat="server" ForeColor="Brown" />
            <br /><br />
            This is the AppRelativeCurrentExecutionFilePath to the current page:<br />
            <asp:Label ID="Label3" runat="server" ForeColor="Brown" /><br />
            Use it to help programatically construct links to resources based on the location of the current page.<br />
            <asp:Image ID="Image2" runat="server" />
            <asp:Label ID="Label4" runat="server" ForeColor="Brown" />
            </div>
        </form>
    </body>
    </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 属性用于将筛选器附加到响应流,该响应流使页面上显示的文本全部大写。
    <%@ Page Language="C#" %>
    <%@ Import Namespace="System.IO" %>
    <%@ import Namespace="Samples.AspNet.CS.Controls"  %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <script runat="server">    private void Page_Load(object sender, EventArgs e)
        {      // Filter the text to be rendered as all uppercase.
          Response.Filter = new UpperCaseFilterStream(Response.Filter);      // Convert a virtual path to a fully qualified physical path.
          string fullpath = Request.MapPath("~\\TestFile.txt");      try
          {
            // Read the contents of the file using a StreamReader.
            using (StreamReader sr = new StreamReader(fullpath))
            while (sr.Peek() >= 0)
            {
              Response.Write((char)sr.Read());
            }
            Message.Text = "Reading the file was successful.";      }
          catch (Exception ex)
          {
            Message.Text = "The process failed.";
          }    
         }</script>
    <html xmlns="http://www.w3.org/1999/xhtml" >
      <head>
        <title>HttpResponse.MapPath Example</title>
      </head>
      <body>
        <form id="form1" runat="server">      <asp:Label id="Message" 
                     runat="server"/>    </form>
      </body>
    </html>示例的第二部分显示一个从 Stream 继承的类,并将响应流中的所有字符都转换为大写。将此代码放置在您的应用程序的 App_Code 文件夹中。using System;
    using System.IO;
    using System.Text;namespace Samples.AspNet.CS.Controls
    {   public class UpperCaseFilterStream : Stream
       // This filter changes all characters passed through it to uppercase.
       {
          private Stream strSink;
          private long lngPosition;      public UpperCaseFilterStream(Stream sink)
          {
              strSink = sink;
          }      // The following members of Stream must be overriden.
          public override bool CanRead
          {
             get { return true; }
          }      public override bool CanSeek
          {
             get { return true; }
          }      public override bool CanWrite
          {
             get { return true; }
          }      public override long Length
          {
             get { return 0; }
          }      public override long Position
          {
             get { return lngPosition; }
             set { lngPosition = value; }
          }      public override long Seek(long offset, System.IO.SeekOrigin direction)
          {
             return strSink.Seek(offset, direction);
          }      public override void SetLength(long length)
          {
             strSink.SetLength(length);
          }      public override void Close()
          {
             strSink.Close();
          }      public override void Flush()
          {
             strSink.Flush();
          }      public override int Read(byte[] buffer, int offset, int count)
          {
             return strSink.Read(buffer, offset, count);
          }      // The Write method actually does the filtering.
          public override void Write(byte[] buffer, int offset, int count)
          {
     byte[] data = new byte[count];
         Buffer.BlockCopy(buffer, offset, data, 0, count);
     string inputstring = Encoding.ASCII.GetString(data).ToUpper();
     data = Encoding.ASCII.GetBytes(inputstring);
         strSink.Write(data, 0, count);      }
       }
    }=======================================================================================================Server.MapPath 返回与 Web 服务器上的指定虚拟路径相对应的物理文件路径。如果 path 为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing),MapPath 方法将返回包含当前应用程序的目录的完整物理路径。下面的示例返回包含指定网站的虚拟目录的物理路径。若要读取代码隐藏模块中的 MapPath,请使用 HttpContext.Current.Server.MapPath。String FilePath;
    FilePath = Server.MapPath("/MyWebSite");
      

  4.   


    HttpRequest 的方法:
    MapPath() 返回类型:字符串
    将请求URL 中提到的 虚拟路径 映射到服务器上 资源的实际物理路径注意:与 Sever 对象的 MapPath 方法的 区别:HttpContext.Current.Response.Write(HttpContext.Current.Request.MapPath("WebForm7.aspx").ToString());
    Response.Write(HttpContext.Current.Request.MapPath("\\"));
    Response.Write("<br>"+Server.HtmlEncode(HttpContext.Current.Request.MapPath("\\WebForm7.aspx").ToString());输出结果如下:
    E:\十三少\Load\WebForm7.aspxE:\十三少\E:\十三少\WebForm7.aspx 
    ==============================================MapPath 方法:
     借助 MapPath 方法 我们可以从 虚拟路径得到 Web 资源
    的 物理路径
     Mapth(string path) 其中 sting 表示 Web 服务上的指定的虚拟路径如果将null 作为路径参数来传递,侧会返回 应用程序所在的目录的物理的全路径如  E:\ 十三少\ Load 
    十三少 是我建的一个虚拟目录,Load是一个应用程序
    Response.Write(Server.MapPath("")) 输出结果是
    E:\十三少\LoadHttpContext.Current.Response.Write(HttpContext.Current.Server.MapPath("WebForm7.aspx").ToString());
    Response.Write(HttpContext.Current.Server.MapPath("\\"));
    Response.Write("<br>"+Server.HtmlEncode(HttpContext.Current.Server.MapPath("\\WebForm7.aspx").ToString());输出结果如下:E:\十三少\Load\WebForm7.aspxE:\十三少\E:\十三少\WebForm7.aspx 
      

  5.   

    很谢谢各位了,我基本都弄清楚了,只是有个地方还不太明白,望解释清楚:Request.FilePath和Request.Path的区别如你们所说,Request.Path是Request.FilePath再加PathInfo吧……这个PathInfo是指什么呢?我调试的时候使页面跳转到"Defulat.aspx?Page=1"然后分别用Request.FilePath和Request.Path查看,以为Request.Path得到的结果应该是"/MySite/Default.aspx?Page=1",每想到跟Request.FilePath一样是"/MySite/Default.aspx",那么所谓的PathInfo到底是什么呢?
      

  6.   

    Request.PathInfo获取具有 URL 扩展名的资源的附加路径信息。
    对于 URL Http://www.contoso.com/virdir/page.html/tail,PathInfo 值为 /tail。