2010-05-21 45 views
1

如何檢索當前網頁的完整url,包括http?例如:https://stackoverflow.com/在ASP.net中獲取當前頁面的URL

+0

如何檢索給定的什麼一個完整的URL? – avakar 2010-05-21 09:36:10

+1

很高興知道你在想什麼樣的輸入。輸入是IP地址還是URL的一部分?否則這將無法回答。 – pyrocumulus 2010-05-21 09:36:54

+2

我正在投票結束,因爲沒有人似乎理解這個問題。 – avakar 2010-05-21 12:14:00

回答

5

也許你的意思是讓當前頁面的網址是什麼?

用途:Request.Url.ToString()

或者,如果你想要一個相對URL轉換爲絕對路徑,我beleive的代碼是這樣的:

Request.Url.Host + Page.ResolveUrl(relativeUrl) 
3

如果您正在查找當前請求上下文中的完整URL,則HttpRequest.Url屬性應該執行此操作。爲了得到一個string表示一個Page內:

string completeUrl = Request.Url.ToString(); 
+0

請爲我指定Request.Url的名稱空間.. 謝謝 – SAK 2010-05-21 10:04:26

+0

'HttpRequest'類位於'System.Web'命名空間中。在我的coe示例中,'Request'是'System.Web.Page'上的一個屬性。一個類似的屬性可用'UserControl'和'HttpContext'。 – 2010-05-21 10:10:16

+0

對不起。我不使用usercontrol ...我在這裏使用類文件..事情是我使用DLL .. – SAK 2010-05-21 10:17:35

2

它已經有一段時間,但:

Request.ServerVariables["Url"]; 

http://msdn.microsoft.com/en-us/library/ms525396(VS.90).aspx

或從1998年連這個參考!

http://www.4guysfromrolla.com/webtech/092298-3.shtml

編輯

http://www.w3schools.com/asp/coll_servervariables.asp

<html> 
<body> 
<p> 
<b>You are browsing this site with:</b> 
<%Response.Write(Request.ServerVariables("http_user_agent"))%> 
</p> 
<p> 
<b>Your IP address is:</b> 
<%Response.Write(Request.ServerVariables("remote_addr"))%> 
</p> 
<p> 
<b>The DNS lookup of the IP address is:</b> 
<%Response.Write(Request.ServerVariables("remote_host"))%> 
</p> 
<p> 
<b>The method used to call the page:</b> 
<%Response.Write(Request.ServerVariables("request_method"))%> 
</p> 
<p> 
<b>The server's domain name:</b> 
<%Response.Write(Request.ServerVariables("server_name"))%> 
</p> 
<p> 
<b>The server's port:</b> 
<%Response.Write(Request.ServerVariables("server_port"))%> 
</p> 
<p> 
<b>The server's software:</b> 
<%Response.Write(Request.ServerVariables("server_software"))%> 
</p> 
</body> 
</html>