- Back to Home »
- C# »
Posted by : Jebastin
Saturday, 4 January 2014
The following code is used to get the URL of the current page.
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
OUTPUT
- string url = HttpContext.Current.Request.Url.AbsoluteUri;
- // http://localhost:1302/TESTERS/Default6.aspx
- string path = HttpContext.Current.Request.Url.AbsolutePath;
- // /TESTERS/Default6.aspx
- string host = HttpContext.Current.Request.Url.Host;
- // 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
- Response.Write("<br/> " + HttpContext.Current.Request.Url.Host);
- Response.Write("<br/> " + HttpContext.Current.Request.Url.Authority);
- Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsolutePath);
- Response.Write("<br/> " + HttpContext.Current.Request.ApplicationPath);
- Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsoluteUri);
- Response.Write("<br/> " + HttpContext.Current.Request.Url.PathAndQuery);
OUTPUT
- localhost
- localhost:60527
- /WebSite1test/Default2.aspx
- /WebSite1test
- http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2
- /WebSite1test/Default2.aspx?QueryString1=1&QuerrString2=2