2010-01-22 65 views

回答

6

如果你只想在網址中的最後/之後的部分,呼籲Uri.LocalPathSystem.IO.Path.GetFileName()方法應該做的伎倆:

System.IO.Path.GetFileName(Request.UrlReferrer.LocalPath); 

如果你想輸出保持在URI查詢字符串信息,使用PathAndQuery屬性:

System.IO.Path.GetFileName(Request.UrlReferrer.PathAndQuery); 
+0

感謝這也工作,只是爲了確認,如果我的網址包含一些查詢字符串,那麼我的結果將是WebForm1.aspx或不 – 2010-01-22 13:49:12

+0

LocalPath將刪除關閉。 – 2010-01-22 14:32:00

+0

非常感謝親愛的您的幫助! – 2010-01-22 14:45:34

0

嘗試在UrlReferrerLocalPath屬性:

Label1.Text = Request.UrlReferrer.LocalPath; 

應該爲您提供不僅是文件名。

編輯:這似乎也包括路徑,所以只適用於root。

在這種情況下,你最好只是用Substring()

string str = Request.UrlReferrer.ToString(); 
Label1.Text = str.Substring(str.LastIndexOf('/')+1); 
+0

謝謝!它給我/data/WebForm1.aspx,但是我只想要WebForm1.aspx。請建議 – 2010-01-22 13:33:24

+0

謝謝!在這種情況下,如果我的url返回http:// localhost:82/data/WebForm1.aspx?test =「hello」。我的意思是,如果URL中有查詢字符串,則上面的子字符串邏輯將起作用 – 2010-01-22 13:40:33

+0

它將包含查詢字符串,因此只需在LocalPath屬性上執行'Substring()'操作。我不認爲LocalPath包含查詢字符串。 – 2010-01-22 13:42:20

相關問題