日期:2014-05-18  浏览次数:20417 次

[玻璃鱼V]初学ASP.NET,问下这几个的区别……
Request.ApplicationPath
Request.FilePath
Request.Path

Request.MapPath()
Server.MapPath()

以上到底有什么区别?最好能举例说明一下……

假设我网站的域名是 http://MySite.com
在http://MySite.com/Sub1/Object1.aspx页面上调用以上的属性和方法分别能得到哪些结果?

最主要还是想知道他们各自的作用和区别之处……

先谢过回答的各位了~

------解决方案--------------------
1.Request.ApplicationPath->当前应用的目录
楼主没有接触过jsp吧,如果接触过jsp就会非常清楚,ApplicationPath指的是当前的application(应用程序)的目录
对应的--例如我的服务器上有两个web应用域名都是MySite.com 一个映射到目录MySite.com/1/ 另一个影射到 http://MySite.com/2/
那么 MySite.com/1/就是第一个应用的ApplicationPath 同理 MySite.com/2/就是第二个应用的ApplicationPath

2.Request.FilePath->对应于iis的虚拟目录
如 URL http://MySite.com/1/index.html/pathinfo
FilePath = /1/index.html

3.Request.Path->当前请求的虚拟路径
Path 是 FilePath 和 PathInfo 尾部的串联。例如 URL http://MySite.com/1/index.html/pathinfo
那么Path = /1/index.html/pathinfo

4.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

以上是个人理解,不知道是否全对
------解决方案--------------------
Request.ApplicationPath 获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径。可从不在根目录中的页或 Web 用户控件中使用此属性构造一个相对于应用程序根目录的 URL。这样,位于其他目录结构级别的页和共享控件可以使用相同的代码链接到应用程序中位于固定位置的资源。
下面的示例使用 ApplicationPath 属性,以编程方式构造一个指向位于应用程序中某个固定位置的资源的路径。引用该资源的页不必与该资源位于同一目录中。
HTML code

<%@ 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>
    </