2009-04-16 36 views
8

我想獲得用戶輸入瀏覽器的確切網址。當然,我可以一直使用類似Request.Url.ToString()但是這並沒有給我我想要在以下情況:獲取用戶輸入瀏覽器的確切網址

http://www.mysite.com/rss

通過上面什麼Request.Url.ToString()會給我的網址是:

http://www.mysite.com/rss/Default.aspx

有誰知道如何做到這一點?

我已經嘗試:

  • Request.Url
  • Request.RawUrl
  • this.Request.ServerVariables["CACHE_URL"]
  • this.Request.ServerVariables["HTTP_URL"]
  • ((HttpWorkerRequest)((IServiceProvider)HttpContext.Current).GetService(typeof(HttpWorkerRequest))).GetServerVariable("CACHE_URL")
  • ((HttpWorkerRequest)((IServiceProvider)HttpContext.Current).GetService(typeof(HttpWorkerRequest))).GetServerVariable("HTTP_URL")
+0

如果您查看Cassini源代碼,您會看到在調用HttpRuntime.ProcessRequest之前,用戶請求的URL將被覆蓋(在某些情況下)。這幾乎排除了任何與HttpWorkerRequest無關的方式。 – 2010-11-29 22:34:43

回答

6

編輯:你想HttpWorkerRequest.GetServerVariable()與關鍵HTTP_URLCACHE_URL。請注意,IIS 5和IIS 6之間的行爲不同(請參閱密鑰的文檔)。

爲了能夠訪問所有服務器變量(如果你null),直接訪問的HttpWorkerRequest:

HttpWorkerRequest workerRequest = 
    (HttpWorkerRequest)((IServiceProvider)HttpContext.Current) 
    .GetService(typeof(HttpWorkerRequest)); 
+0

這不起作用。它仍然給我http://www.mysite.com/rss/Default.aspx – 2009-04-16 20:00:46

+0

我試過(Request.ServerVariables [「HTTP_URL」]),我得到空... – 2009-04-16 20:08:36

+0

不工作。 ((HttpWorkerRequest)((IServiceProvider)HttpContext.Current).GetService(typeof(HttpWorkerRequest)))。GetServerVariable(「CACHE_URL」)and((HttpWorkerRequest)((IServiceProvider)HttpContext.Current).GetService (HttpWorkerRequest)))。GetServerVariable(「HTTP_URL」) – 2009-04-16 20:38:59

0
Request.RawUrl 

我想是你是後猴子...

4

還請記住,「用戶輸入的確切URL」在服務器上可能永遠不可用。從手指到服務器的鏈中的每個鏈接都可以稍微修改請求。

例如,如果我在瀏覽器窗口中鍵入xheo.com,IE會自動轉換爲http://www.xheo.com。然後,當請求到達IIS時,它向瀏覽器說 - 你真的想要默認頁面http://www.xheo.com/Default.aspx。所以瀏覽器通過詢問默認頁面來響應。

HTTP 30x重定向請求發生同樣的事情。服務器可能只會看到瀏覽器提出的最終請求。

3

嘗試使用Request.Url.OriginalString 可能會給你你正在尋找的東西。

0
做到這一點

最簡單方法是使用客戶端的編程提取的準確網址:

<script language="javascript" type="text/javascript"> 
document.write (document.location.href); 
</script> 
0

這是可能的,你只需要從請求對象結合了幾個值的重建確切網址進入:

Dim pageUrl As String = String.Format("{0}://{1}{2}", 
    Request.Url.Scheme, 
    Request.Url.Host, 
    Request.RawUrl) 

Response.Write(pageUrl) 

輸入地址http://yousite.com/?hello恰好返回:

http://yousite.com/?hello 
相關問題