Posted by : Jebastin Saturday 4 January 2014

The following code is used to get the URL of the current page.

  1. string url = HttpContext.Current.Request.Url.AbsoluteUri;
  2. // http://localhost:1302/TESTERS/Default6.aspx

  1. string path = HttpContext.Current.Request.Url.AbsolutePath;
  2. // /TESTERS/Default6.aspx

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

You may need to get different values from URL.

Below example shows different ways of extracting different parts of URL

EXAMPLE (Sample URL)

http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2

  1. Response.Write("<br/> " + HttpContext.Current.Request.Url.Host);
  2. Response.Write("<br/> " + HttpContext.Current.Request.Url.Authority);
  3. Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsolutePath);
  4. Response.Write("<br/> " + HttpContext.Current.Request.ApplicationPath);
  5. Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsoluteUri);
  6. Response.Write("<br/> " + HttpContext.Current.Request.Url.PathAndQuery);

OUTPUT
  1. localhost
  2. localhost:60527
  3. /WebSite1test/Default2.aspx
  4. /WebSite1test
  5. http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2
  6. /WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Link To This Post/Page

Spread The Word

Add this button to your blog:
JJ Technology Solutions

Blog Archive

Trackers

eXTReMe Tracker
facebook

- Copyright © JJ Technology Solutions - Powered by Source Code Solutions -