1.簡單的環境搭建
在本地IIS上配置了一個網站:主機名稱為wjnhome.com,連接埠88,然後建了一個虛擬目錄指向同一網站,虛擬目錄名稱為virtual,配置host為127.0.0.1 wjnhome.com
所以地址就為:http://jb51.net:88/virtual/urldemo.aspx?id=2#top
2.編寫簡單的代碼
複製代碼 代碼如下://虛擬目錄的路徑
Response.Write("<strong>Request.ApplicationPath:</strong>" + Request.ApplicationPath + "</br>");
//網站的實體路徑(完整路徑)
Response.Write("<strong>Request.PhysicalPath:</strong>" + Request.PhysicalPath + "</br>");
//網站實體路徑的目錄
Response.Write("<strong>DirectoryName:</strong>" + System.IO.Path.GetDirectoryName(Request.PhysicalPath) + "</br>");
//網站實體路徑的目錄
Response.Write("<strong>Request.PhysicalApplicationPath:</strong>" + Request.PhysicalApplicationPath + "</br>");
//當前頁面的檔案名稱
Response.Write("<strong>FileName:</strong>" + System.IO.Path.GetFileName(Request.PhysicalPath) + "</br>");
//當前頁面的虛擬路徑
Response.Write("<strong>Request.CurrentExecutionFilePath:</strong>" + Request.CurrentExecutionFilePath + "</br>");
//當前頁面的虛擬路徑
Response.Write("<strong>Request.FilePath:</strong>" + Request.FilePath + "</br>");
Response.Write("<strong>Request.Path:</strong>" + Request.Path + "</br>");
//原始URL
Response.Write("<strong>Request.RawUrl:</strong>" + Request.RawUrl + "</br>");
//絕對路徑(不包括參數什麼的)
Response.Write("<strong>Request.Url.AbsolutePath:</strong>" + Request.Url.AbsolutePath + "</br>");
//絕對URL
Response.Write("<strong>Request.Url.AbsoluteUri:</strong>" + Request.Url.AbsoluteUri + "</br>");
//URL協議方案
Response.Write("<strong>Request.Url.Scheme:</strong>" + Request.Url.Scheme + "</br>");
//URL的主機名稱
Response.Write("<strong>Request.Url.Host:</strong>" + Request.Url.Host + "</br>");
//URL連接埠號碼
Response.Write("<strong>Request.Url.Port:</strong>" + Request.Url.Port + "</br>");
//主機名稱+連接埠號碼
Response.Write("<strong>Request.Url.Authority:</strong>" + Request.Url.Authority + "</br>");
//擷取檔案名稱的本地作業系統表現形式
Response.Write("<strong>Request.Url.LocalPath:</strong>" + Request.Url.LocalPath + "</br>");
//附加路徑資訊,例如http://jb51.net:88/UrlDemo.aspx/Hello?id=22#top 那麼這裡就是Hello
Response.Write("<strong>Request.PathInfo:</strong>" + Request.PathInfo + "</br>");
//URL的路徑和GET參數
Response.Write("<strong>Request.Url.PathAndQuery:</strong>" + Request.Url.PathAndQuery + "</br>");
//URL的GET參數
Response.Write("<strong>Request.Url.Query:</strong>" + Request.Url.Query + "</br>");
//主要指的是http://jb51.net:88/UrlDemo.aspx/Hello?id=22#top中#後面的top。
//但一般情況下無法擷取值,因為瀏覽器不會把這個值發送到伺服器端
Response.Write("<strong>Request.Url.Fragment:</strong>" + Request.Url.Fragment + "</br>");
//主機名稱
Response.Write("<strong>Request.Url.DnsSafeHost:</strong>" + Request.Url.DnsSafeHost + "</br>");
//URL的全部
Response.Write("<strong>Request.Url.OriginalString:</strong>" + Request.Url.OriginalString + "</br>");
//這種情況 Uri uriAddress = new Uri ("http://user:password@www.contoso.com/index.htm ") Console.WriteLine(uriAddress.UserInfo);
Response.Write("<strong>Request.Url.UserInfo:</strong>" + Request.Url.UserInfo + "</br>");
//從某個頁面跳轉過來的時候會顯示源頁面的值
Response.Write("<strong>Request.UrlReferrer:</strong>" + Request.UrlReferrer + "</br>");
//URI的每一段
for (var i = 0; i < Request.Url.Segments.Length;i++ )
{
Response.Write("<strong>Request.Url.Segment" + i + ":</strong>" + Request.Url.Segments[i] + "</br>");
}
3.輸出結果
Request.ApplicationPath:/virtual
Request.PhysicalPath:E:\VsProject\201200420\UrlDemo\UrlDemo\urldemo.aspx
DirectoryName:E:\VsProject\201200420\UrlDemo\UrlDemo
Request.PhysicalApplicationPath:E:\VsProject\201200420\UrlDemo\UrlDemo\
FileName:urldemo.aspx
Request.CurrentExecutionFilePath:/virtual/urldemo.aspx
Request.FilePath:/virtual/urldemo.aspx
Request.Path:/virtual/urldemo.aspx
Request.RawUrl:/virtual/urldemo.aspx?id=2
Request.Url.AbsolutePath:/virtual/urldemo.aspx
Request.Url.AbsoluteUri:http://jb51.net:88/virtual/urldemo.aspx?id=2
Request.Url.Scheme:http
Request.Url.Host:wjnhome.com
Request.Url.Port:88
Request.Url.Authority:wjnhome.com:88
Request.Url.LocalPath:/virtual/urldemo.aspx
Request.PathInfo:
Request.Url.PathAndQuery:/virtual/urldemo.aspx?id=2
Request.Url.Query:?id=2
Request.Url.Fragment:
Request.Url.DnsSafeHost:wjnhome.com
Request.Url.OriginalString:http://jb51.net:88/virtual/urldemo.aspx?id=2
Request.Url.UserInfo:
Request.UrlReferrer:
Request.Url.Segment0:/
Request.Url.Segment1:virtual/
Request.Url.Segment2:urldemo.aspx
作者:wjn