0

I have used URL rewriting. I have one question is that I have URL like

http://localhost/learnmore.aspx

This is URL overwriting so I want this complete URL so I have coded like this.

string url=Request.RawUrl;

After this code I got /learnmore.aspx in url variable but I want the complete URL http://localhost/learnmore.aspx

How can I do this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kartik Patel
  • 9,303
  • 12
  • 33
  • 53
  • don't see how this is related to Url Rewriting, you just wont a full URL ? Request.Url.AbsoluteUri ? – Antonio Bakula Apr 20 '12 at 09:49
  • Yes but if i use the Request.Url.AbsoluteUri then i get http://localhost/Default.aspx?pageid=25 so its not useful for me and thats due to URL ReWritting – Kartik Patel Apr 20 '12 at 10:02

4 Answers4

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

EDIT: To remove query string items: (found from 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);

OR

string url = "http://www.somesite.com/mypage.aspx?myvalue1=hello&myvalue2=goodbye";
string path = url.Substring(0, url.IndexOf("?"));
Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • I have used the URLRewritting here so i will get something like this if i used the above three ...i get http://localhost/Default.aspx?pageid=25 but i want http://localhost/learnmore.aspx – Kartik Patel Apr 20 '12 at 10:01
  • @KartikPatel, I have edited the answer, just try the solution now – Habib Apr 20 '12 at 10:08
1

You can get host and scheme this way :

Request.Url.GetLeftPart(UriPartial.Authority)

With this you will get :

http://localhost/

and then you can append RawUrl

Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
0

you can try to get the base url :

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + 
    Request.ApplicationPath.TrimEnd('/') + "/";
Zaki
  • 5,540
  • 7
  • 54
  • 91
0

Request.Url.AbsoluteUri would do the trick.

Xharze
  • 2,703
  • 2
  • 17
  • 30