首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> ASP.NET 基礎

ASP.NET 取得當前 URL 各屬性完整對照表

     編輯:ASP.NET 基礎

針對 `Request.Url`、`Request.RawUrl` 等屬性,以實例網址 `https://www.example.com/app/page.aspx?id=1&name=test` 逐一對照輸出值,方便快速查閱。

假設環境

  • 完整網址:`https://www.example.com/app/page.aspx?id=1&name=test`
  • 應用程式虛擬路徑:`/app`
  • 實體路徑:`C:\Webs\MyApp\page.aspx`

屬性對照表

屬性/方法回傳值說明
Request.Url.ToString()`https://www.example.com/app/page.aspx?id=1&name=test`完整 URL (含 QueryString)
Request.RawUrl`/app/page.aspx?id=1&name=test`相對根目錄的路徑 + QueryString
Request.Url.PathAndQuery`/app/page.aspx?id=1&name=test`同 RawUrl
Request.Url.AbsolutePath`/app/page.aspx`相對根目錄的路徑 (不含 QueryString)
Request.Path`/app/page.aspx`同 AbsolutePath
Request.CurrentExecutionFilePath`/app/page.aspx`執行中檔案虛擬路徑 (Server.Transfer 後不同)
Request.ApplicationPath`/app`應用程式虛擬根路徑
Request.Url.Host`www.example.com`主機名 (不含 Port)
Request.Url.Authority`www.example.com:80`主機 + Port
Request.Url.Scheme`http`協定
Request.Url.Port`80`Port 號
Request.Url.Query`?id=1&name=test`QueryString (含前導 ?)
Request.QueryString["id"]`1`解析後的參數值
Request.Url.Fragment`""`片段識別符 (# 後內容)
Request.PhysicalPath`C:\Webs\MyApp\page.aspx`伺服器實體檔案路徑
Request.Url.LocalPath`/app/page.aspx`同 AbsolutePath
Request.Url.AbsoluteUri`https://www.example.com/app/page.aspx?id=1&name=test`同 ToString()
Request.PathInfo`""`路徑資訊 (如 `/page.aspx/extra` 中的 `/extra`)

常見應用場景

  • 重導並保留 QueryString:`Response.Redirect(Request.RawUrl)` 或 `Request.Url.PathAndQuery`。
  • 判斷是否為根目錄:`Request.Path == Request.ApplicationPath` 或 `Request.Url.AbsolutePath == Request.ApplicationPath`。
  • 建立絕對連結:`new Uri(Request.Url, "~/other.aspx").ToString()`。
  • 取得不含 QueryString 的當前頁 URL:`Request.Url.GetLeftPart(UriPartial.Path)`。

Server.Transfer 後的差異

`Request.Path`、`Request.CurrentExecutionFilePath`、`Request.Url.AbsolutePath` 會反映目標頁面路徑,但 `Request.RawUrl`、`Request.Url.PathAndQuery` 保持原始請求路徑。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved