Wednesday, April 8, 2015

How to get the Current URL of the page using C#



How to get the Current URL of the page using C#

Below example shows different ways of extracting different parts of URL
EXAMPLE (Sample URL)
http://localhost:1234/WebSite1/Default2.aspx?QueryString1=1&QuerrString2=2
CODE
Response.Write("
 " + HttpContext.Current.Request.Url.Host);
Response.Write("
 " + HttpContext.Current.Request.Url.Authority);
Response.Write("
 " + HttpContext.Current.Request.Url.AbsolutePath);
Response.Write("
 " + HttpContext.Current.Request.ApplicationPath);
Response.Write("
 " + HttpContext.Current.Request.Url.AbsoluteUri);
Response.Write("
 " + HttpContext.Current.Request.Url.PathAndQuery);
OUTPUT
localhost
localhost:1234
/WebSite1/Default2.aspx
/WebSite1
http://localhost:1234/WebSite/Default2.aspx?QueryString1=1&QuerrString2=2
/WebSite/Default2.aspx?QueryString1=1&QuerrString2=2