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?

2
  • don't see how this is related to Url Rewriting, you just wont a full URL ? Request.Url.AbsoluteUri ? Commented Apr 20, 2012 at 9:49
  • Yes but if i use the Request.Url.AbsoluteUri then i get localhost/Default.aspx?pageid=25 so its not useful for me and thats due to URL ReWritting Commented Apr 20, 2012 at 10:02

4 Answers 4

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("?"));
Sign up to request clarification or add additional context in comments.

2 Comments

I have used the URLRewritting here so i will get something like this if i used the above three ...i get localhost/Default.aspx?pageid=25 but i want localhost/learnmore.aspx
@KartikPatel, I have edited the answer, just try the solution now
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

Comments

0

you can try to get the base url :

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

Comments

0

Request.Url.AbsoluteUri would do the trick.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.