Kartik Patel
Kartik Patel

Reputation: 9603

How to get complete URL of browser in UrlRewriting using C#

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?

Upvotes: 0

Views: 5228

Answers (4)

Antonio Bakula
Antonio Bakula

Reputation: 20693

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

Upvotes: 1

Habib
Habib

Reputation: 223207

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("?"));

Upvotes: 3

Xharze
Xharze

Reputation: 2733

Request.Url.AbsoluteUri would do the trick.

Upvotes: 0

Zaki
Zaki

Reputation: 5600

you can try to get the base url :

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + 
    Request.ApplicationPath.TrimEnd('/') + "/";

Upvotes: 0

Related Questions