[玻璃鱼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> </form></body></html>
[解决办法]
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.aspx
E:\十三少\
E:\十三少\WebForm7.aspx
==============================================
MapPath 方法:
借助 MapPath 方法 我们可以从 虚拟路径得到 Web 资源
的 物理路径
Mapth(string path) 其中 sting 表示 Web 服务上的指定的虚拟路径
如果将null 作为路径参数来传递,侧会返回 应用程序所在的目录的物理的全路径
如 E:\ 十三少\ Load
十三少 是我建的一个虚拟目录,Load是一个应用程序
Response.Write(Server.MapPath("")) 输出结果是
E:\十三少\Load
HttpContext.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.aspx
E:\十三少\
E:\十三少\WebForm7.aspx
[解决办法]