天天看點

ASP.net擷取目前url各種屬性(檔案名、參數、域名 等)的方法

假設目前頁完整位址是:http://www.test.com/aaa/bbb.aspx?id=5&name=kelli

"http://"是協定名

"www.test.com"是域名

"aaa"是站點名

"bbb.aspx"是頁面名(檔案名)

"id=5&name=kelli"是參數

【1】擷取 完整url (協定名+域名+站點名+檔案名+參數)

string url=Request.Url.ToString();

url= http://www.test.com/aaa/bbb.aspx?id=5&name=kelli

【2】擷取 站點名+頁面名+參數:

string url=Request.RawUrl;

(或 string url=Request.Url.PathAndQuery;)

url= /aaa/bbb.aspx?id=5&name=kelli

【3】擷取 站點名+頁面名:

string url=HttpContext.Current.Request.Url.AbsolutePath;

(或 string url= HttpContext.Current.Request.Path;)

url= aaa/bbb.aspx

【4】擷取 域名:

string url=HttpContext.Current.Request.Url.Host;

url= www.test.com

【5】擷取 參數:

string url= HttpContext.Current.Request.Url.Query;

url= ?id=5&name=kelli

繼續閱讀