2012-04-20 59 views
0

我已經使用了URL重寫。我有一個問題是,我有像URL一樣的網址如何使用C#在UrlRewriting中獲取瀏覽器的完整URL

http://localhost/learnmore.aspx 

這是URL覆蓋,所以我想這個完整的URL,所以我有這樣的代碼。

string url=Request.RawUrl; 

這個代碼我在url變量/learnmore.aspx後,但我要的完整URL http://localhost/learnmore.aspx

我怎樣才能做到這一點?

+0

不要」 t看看這是如何與Url重寫相關的,你只需要一個完整的URL? Request.Url.AbsoluteUri? – 2012-04-20 09:49:19

+0

是的,但如果我使用Request.Url.AbsoluteUri然後我得到http://localhost/Default.aspx?pageid = 25所以它對我沒有用,那是由於URL ReWritting – 2012-04-20 10:02:41

回答

1

你可以得到主機和方案是這樣的:

Request.Url.GetLeftPart(UriPartial.Authority) 

有了這個,你將獲得:

http://localhost/

,然後你可以追加RawUrl

0

你可以嘗試獲取基本網址:

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + 
    Request.ApplicationPath.TrimEnd('/') + "/"; 
3
string url = HttpContext.Current.Request.Url.AbsoluteUri; 
// http://localhost/learnmore.aspx 

string path = HttpContext.Current.Request.Url.AbsolutePath; 
// /localhost/learnmore.aspx 

string host = HttpContext.Current.Request.Url.Host; 
// localhost 

編輯:要刪除查詢字符串的項目:(從Get url without querystring找到)

var uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri); 
string path = uri.GetLeftPart(UriPartial.Path); 

OR

Uri url = new Uri("http://www.somesite.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye"); 
string path = String.Format("{0}{1}{2}{3}", url.Scheme, 
    Uri.SchemeDelimiter, url.Authority, url.AbsolutePath); 

string url = "http://www.somesite.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye"; 
string path = url.Substring(0, url.IndexOf("?")); 
+0

我在這裏使用了URLRewritting,所以我會得到這樣的東西,如果我使用上述三...我得到http://localhost/Default.aspx?pageid = 25,但我想要http://localhost/learnmore.aspx – 2012-04-20 10:01:26

+0

@KartikPatel,我編輯了答案,現在就試試解決方案 – Habib 2012-04-20 10:08:26

+0

謝謝Habib ......現在工作的很好...... – 2012-04-20 10:13:43

0

Request.Url.AbsoluteUri會做的伎倆。

相關問題